Both classes are precompiled. I run FromWhich.java and expect to see on the screen the answer "Hello World"

public class FromWhich { public static void main(String[] args) throws IOException { Runtime runtime = Runtime.getRuntime(); String cmd = "CMD /C java WhichToStart"; Process process = runtime.exec(cmd.split(" ")); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } 

}

 public class WhichToStart { public void start() { System.out.println("Hello World"); } public static void main(String[] args) { new WhichToStart().start(); } 

}

    3 answers 3

    I usually did something like this:

      final Process p = Runtime.getRuntime().exec(/*вписать нужное*/); final ProcessResultReader stderr = new ProcessResultReader(p.getErrorStream(), "STDERR"); final ProcessResultReader stdout = new ProcessResultReader(p.getInputStream(), "STDOUT"); stderr.start(); stdout.start(); final int exitValue = p.waitFor(); System.out.print(stdout.toString()); System.err.print(stderr.toString()); class ProcessResultReader extends Thread { final InputStream is; final String type; final StringBuilder sb; ProcessResultReader(final InputStream is, String type) { this.is = is; this.type = type; this.sb = new StringBuilder(); } public void run() { try { final InputStreamReader isr = new InputStreamReader(is); final BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (final IOException ioe) { System.err.println(ioe.getMessage()); throw new RuntimeException(ioe); } } @Override public String toString() { return this.sb.toString(); } } 
    • It seems to me that this is essentially the same code. Could you please provide the code of the program being called, which will return something. How should it look and does it need (in the second, called program) to redirect output streams in some way so that the caller can process them? - Roberto
    • An example of a call from above, you do not need to redirect anything, just output to the console. - pavel
    • I apologize for getting you out, but what needs to be indicated here: final Process p = Runtime.getRuntime (). Exec (/ * enter the required * /); on the spot / * enter the necessary * /, intuition suggests that this is exactly the case. - Roberto
    • one
      yes anything ... java ClassToStart for example. - pavel
     import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; class ProcessResultReader extends Thread { final InputStream is; final String type; final StringBuilder sb; /** * * @param is * @param type */ ProcessResultReader( final InputStream is, String type) { this.is = is; this.type = type; this.sb = new StringBuilder(); } public void run() { try { final InputStreamReader isr = new InputStreamReader(is); final BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { this.sb.append(line).append("\n"); } } catch (final IOException ioe) { System.err.println(ioe.getMessage()); throw new RuntimeException(ioe); } } @Override public String toString() { return this.sb.toString(); } } 

    Upper class is used below

     package main.java.utils; import java.io.File; import java.util.concurrent.TimeUnit; public class ProcessManager { private ProcessResultReader stderrReader; private ProcessResultReader stdoutReader; private Process p; public int exitCode = Integer.MIN_VALUE; public String command; public ProcessManager(String command) { this.command = command; this.stderrReader = null; this.stdoutReader = null; } private void runCommand(boolean waitEnding, long timeout, boolean isCmd, File dir) { try { if (isCmd) if (dir != null) p = Runtime.getRuntime().exec(String.format("cmd /c %s", command), null, dir); else p = Runtime.getRuntime().exec(String.format("cmd /c %s", command)); else if (dir != null) p = Runtime.getRuntime().exec(command, null, dir); else p = Runtime.getRuntime().exec(command); stderrReader = new ProcessResultReader(p.getErrorStream(), "STDERR"); stdoutReader = new ProcessResultReader(p.getInputStream(), "STDOUT"); stderrReader.start(); stdoutReader.start(); if (waitEnding) exitCode = p.waitFor(); if (timeout > 0) p.waitFor(timeout, TimeUnit.SECONDS); } catch (Exception e) { Messages.getInstance().fatal(e.getMessage(), e); } finally { if (waitEnding) p.destroy(); //assertEquals("Process exit code not valid!", 0, exitCode); } } public void runCommand(long timeout) { this.runCommand(true, timeout, true, null); } public void runCommand() { this.runCommand(true, 0, true, null); } public void runCommand(File workingDir) { this.runCommand(true, 0, true, workingDir); } public void runCommand(boolean waitEnding, boolean isCmd) { this.runCommand(waitEnding, 0, isCmd, null); } public void runCommand(boolean waitEnding, boolean isCmd, File workingDir) { this.runCommand(waitEnding, 0, isCmd, workingDir); } public void destroy() { p.destroy(); } public String getStdout() { if (stdoutReader != null) { return stdoutReader.sb.toString(); } return ""; } public String getStdError() { if (stderrReader != null) { return stderrReader.sb.toString(); } return ""; } } 

    Examples:

     ProcessManager pm = new ProcessManager("ping 8.8.8.8"); pm.runCommand(true, true); String sb = processManager.getStdout() + processManager.getStdError(); ProcessManager pm = new ProcessManager("java WhichToStart"); pm.runCommand(true, false); String sb = processManager.getStdout() + processManager.getStdError(); 
       public class FromWhich { public static void main(String[] args) throws IOException, InterruptedException { ProcessBuilder processBuilder = new ProcessBuilder("CMD /C java -cp ./ WhichToStart.class".split(" ")); processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } }