I am using Java's ProcessBuilder to start a subprocess, which is another Java program that has to run in a separate JVM.
I start two Threads to read from the stdout and stderr streams from the Process, so that there is no hang if the stream buffers are full. The call to Process.waitFor returns but the streams aren't terminated.
The code I am using looks something like (command is a list of strings):
ProcessBuilder pb = new ProcessBuilder(command);
final Process p = pb.start();
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
final ByteArrayOutputStream errStream = new ByteArrayOutputStream();
Thread outputThread = new Thread() {
@Override
public void run() {
try {
IOUtils.copy(p.getInputStream(), outStream);
} c开发者_JAVA百科atch (IOException e) {
e.printStackTrace();
}
};
};
outputThread.start();
Thread errorThread = new Thread() {
@Override
public void run() {
try {
IOUtils.copy(p.getErrorStream(), errStream);
} catch (IOException e) {
e.printStackTrace();
}
};
};
errorThread.start();
int returncode = p.waitFor();
outputThread.join();
errorThread.join();
If I run something else, such as "java -version" or "dir" or something, the code works fine. I have access to the Java code that I am trying to run, but I have never heard that you should call close() on System.out.
Apache commons exec does all this for you. A lot more easier to do...
http://commons.apache.org/exec/
As I know from this website you must close all std-streams from Process object on your own. Regardless if used before or not. This seems highly related to G1 garbage collector (default since Java 7 afaik) which keeps pipes opened when not closed explicitly - even when the subprocess terminated.
I am not familiar with the internals here but the code I posted in my answer to my question works fine on a 24/7 system calling GhostScript and others a lot.
Other questions and answers on SO stating that you must close Process' std-streams explicit:
- Process Builder waitFor() issue and Open file limitations
精彩评论