Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process output is too big

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.

like image 241
Tamás Juhász Avatar asked Feb 17 '15 17:02

Tamás Juhász


1 Answers

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.

like image 51
Tamás Juhász Avatar answered Oct 18 '22 23:10

Tamás Juhász