Please see my answer https://stackoverflow.com/a/32342557/5226711:
The I/O redirection of
ProcessBuilder
does not redirect streams but file descriptors. Even settingSystem.out
to another stream does not change the file descriptor of the initial standard output.ProcessBuilder.inheritIO()
inherits the parent process' standard output/input/error file descriptors.The
PrintStream
instance is not passed to the started operating system process, but rather something like (it is pseudo code, I do not think it actually works):((FileOutputStream) System.out.out).getFD()
Or, more correct:
FileDescriptor.out
which is
static final
indicating that it will not change, even if you setSystem.out
to something different.
Regarding your question, this means you have to use a File
and ProcessBuilder.redirectOutput(File)
instead of a PrintStream
:
ProcessBuilder pb = new ProcessBuilder("... some args ...");
pb.redirectOutput(new File("stdout.txt"));
pb.start().waitFor();
This redirects only the output of the child process to stdout.txt.
If you want to direct all output from both the parent and the child process, you have to redirect the parent's System.out to the file and capture the child's output in the parent and output it to its redirected System.out:
System.setOut(new PrintStream(new File("stdout.txt")));
ProcessBuilder pb = new ProcessBuilder("... some args ...");
Process p = pb.start();
BufferedReader childOutput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = output.readLine()) != null) {
System.out.println(line); // child`s output goes to stdout.txt
}