Process output is too big
Asked Answered
A

1

7

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.

Albuquerque answered 17/2, 2015 at 17:50 Comment(0)
A
4

After a week I decided to share what I did in this situation. Better answers still appreciated.

It's like a workaround, but works pretty well. You can add this line:

 cmd += " >> " + outputFileName;

This way the cmd on Windows and the bash on Mac (Unix too) will do the work and skip the java part in the logging process. Also you can read the file back with an other method, which can handle this big amount of data, and use the output anywhere in your code.

Hope this answer will be useful.

Albuquerque answered 24/2, 2015 at 13:39 Comment(1)
I hit a similar issue but the answer you propose above is causing me issues on Linux because I need to use many threads and it blocks them, anyway thanks for sharingVasoconstrictor

© 2022 - 2024 — McMap. All rights reserved.