This may be a trivial question but I can't easily find an answer. I've got a simple program in Java:
System.setOut(new PrintStream(new File("stdout.txt")));
...
...
ProcessBuilder pb = new ProcessBuilder("... some args ...");
pb.inheritIO();
pb.start().waitFor();
My intention is to store all output of the process (including child pb
) in the stdout.txt
. But, this seem not to work and the output of pb
is redirected to my process parent's standard output as if System.setOut(...)
was never called.
Is there any way I can use pb.inheritIO/pb.inheritOutput
methods to redirect to a redirected System.out
? Am I missing something obvious? I know I can do it the old way by reading from the child's standard output manually in a thread but that's just more hassle.
Cheers, Jacek
Please see my answer https://stackoverflow.com/a/32342557/5226711:
Regarding your question, this means you have to use a
File
andProcessBuilder.redirectOutput(File)
instead of aPrintStream
: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: