I have a Java tool with the following code:
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
// any error message?
StreamGobblerLocal errorGobbler = new StreamGobblerLocal(proc.getErrorStream(), "ERROR");
// any output?
StreamGobblerLocal outputGobbler = new StreamGobblerLocal(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
commandResultBean.setCLI_ReturnValue(exitVal);
It works, but when the output of the subprocess is big, it deadlocks. I almost sure the problem is here the following:
Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.
I found it here: http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html
How can I solve the problem? Big output is like 30000 rows in Notepad, and I have to log these lines.
My code was copied from this article, so you can check the implementations I didn't copied here.
http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html
Please note that I'm not the first developer of the tool, so I can hardly answer questions like "Why are you using this" or similar questions.
Any help appreciated, thanks in advance.