1

I use the following code, for redirecting the output of a process I launch from my Java app:

ProcessBuilder builder = new ProcessBuilder("MyProcess.exe");
builder.redirectOutput(Redirect.INHERIT);
builder.redirectErrorStream(true);

Now, this works fine when I run the code from eclipse - I can see the output in Eclipse's console.
Yet when I create a jar file and run it from a cmd window, e.g. java -jar MyJar.jar, it doesn't print the output of the process. What could be the reason for this?

2 Answers 2

2

I know I'm late in answering, but I came across this question before coming across the answer, and wanted to save anybody else in the same boat some searching.

This is actually a known bug for Windows: https://bugs.openjdk.java.net/browse/JDK-8023130

You can get around it by redirecting the streams yourself:

Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
   System.out.println(line);
}

p.waitFor();

br.close();
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't manage to test it yet, as this is code from my previous job... :-) But it seems indeed like the issue in question. Thanks!
0

It may be, that process is printing an error and exiting for some reason. So, the actual output goes into Err stream and not into the Out stream. Your code redirects Out stream only, so important process error information may be lost. I would suggest to inherit both Out and Err streams using this code:

ProcessBuilder builder = new ProcessBuilder("MyProcess.exe");
builder.inheritIO();

One more reason to redirect both streams is related to the output buffering for child process. If parent process (your java application) is not reading or redirecting standard streams (Out and Err) of the child process, then the latter may be blocked after a while, unable to make any further progress.

It definitely wouldn't hurt to have possible errors in the output anyway.

4 Comments

Thanks, I actually forgot to add that I also have the line builder.redirectErrorStream(true);. I also tried replacing those two methods with the one you suggested, but the result is the same.
Also, the inner process seems to run without errors (according to what I can see through other metrics, e.g. run time).
Are you able to see System.out.println() output in cmd window? Chanses are System.out itself is redirected somewhere, e.g. in log file.
Yes, I am able to see System.out output. I have the following code: Process proc = builder.start(); int result = proc.waitFor(); System.out.println(result); This code prints 0, as in the process exited with code 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.