Tuesday, April 29, 2014

Redirect process output to std output when you fork one process from another process.

In the situation where you want to redirect input / output of one process to the std input output of parent process (from which you are starting first process). You can make use of inheritIO() method of a ProcessBuilder.
ProcessBuilder pb = new ProcessBuilder("command", "argument);
pb.directory(new File(<directory from where you want to run the command>));
Process p = pb.start();

It may not be necessary to call waitFor() method in above code. This will just help to wait current process to wait for this child process to get complete.

There exist few other options as well, through which you can redirect required stream to standard output and input.


1 comment: