Late answer; but you can use a named pipe. Create one first via mkfifo
. Then in your Java code:
ProcessBuilder pb1 = new ProcessBuilder("gunzip", "-c", "log.gz");
ProcessBuilder pb2 = new ProcessBuilder("grep", "error");
File pipe = new File(NAMED_PIPE_PATH); // NAMED_PIPE_PATH points to what you created via mkfifo
pb1.redirectOutput(ProcessBuilder.Redirect.to(pipe));
pb2.redirectInput(ProcessBuilder.Redirect.from(pipe));
ProcessStartThread pst1 = new ProcessStartThread(pb1);
ProcessStartThread pst2 = new ProcessStartThread(pb2);
pst1.start();
pst2.start();
Note that ProcessStartThread is a simple custom class that extends Thread and basically does nothing other than call start() on the passed ProcessBuilder instance. The reason this has to be done from a separate thread is that ProcessBuilder.start() will hang until the other end of the named pipe is connected (it needs to open input/output to start execution).
edit: here is the ProcessStartThread
class also:
class ProcessStartThread extends Thread {
private ProcessBuilder procBuilder;
private Process proc;
public ProcessStartThread(ProcessBuilder procBuilder) {
this.procBuilder = procBuilder;
this.proc = null;
}
public void run() {
try { this.proc = this.procBuilder.start(); }
catch ( Exception e ) { e.printStackTrace(); }
}
public Process getProcess() {
return this.proc;
}
}