user1111929
user1111929

Reputation: 6099

Java process handling: keep correct order with output+error stream?

Assume that I am running a process and I want to redirect its streams via the classical technique:

ProcessBuilder pb = new ProcessBuilder("C:/folder/script.bat");
Process p = pb.start();
InputStreamReader out = p.getInputStream();
InputStreamReader err = p.getErrorStream();
OutputStreamWriter in = p.getOutputStream();    

Now my question: I want to display all output in black, and all errors in red, and I want to maintain the correct order.

So neither work as expected. How should I display output/errors in different colors, in the correct order?

Upvotes: 1

Views: 386

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Your problem is that with your techniques you're guaranteed to block one or the other stream since you're listening to both of them on the same thread. You should listen to them on different threads and simply display them when data comes in through them.

Upvotes: 2

Related Questions