Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting Output of ProcessBuilder in Java 5/6?

I am looking for a way to redirect the output of a Process / ProcessBuilder? I know that it works in Java 7 like this:

ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectOutput();
Process process = builder.start();

But I need the same for Java 5/6 ... Any help highly appreciated.

like image 999
salocinx Avatar asked Mar 03 '12 22:03

salocinx


1 Answers

Sample code for cmd process on Windows 7, working with Java 6:

ProcessBuilder processBuilder = new ProcessBuilder( "cmd" );        
Process process = processBuilder.start();
OutputStream stream = process.getOutputStream();

Javadoc for getOutputStream() method: says "Gets the output stream of the subprocess. Output to the stream is piped into the standard input stream of the process represented by this Process object."

To redirect the output of a process, I think you can use stream object defined in the code above. You can write it to console etc.

like image 129
Juvanis Avatar answered Nov 05 '22 17:11

Juvanis