Java process inputstream in thread

喜欢而已 提交于 2020-01-06 01:46:19

问题


I develop an Eclipse plugin and I have a problem

My code is the following one:

String run_pelda = "cmd.exe /C pelda.exe";
Runtime pelda_rt = Runtime.getRuntime();
Process pelda_proc = javacheckgen_rt.exec(run_pelda);

And after I would like to read the inputstream:

InputStream toolstr = tool_proc.getInputStream();
InputStreamReader r = new InputStreamReader(toolstr);
BufferedReader in = new BufferedReader(r);

But my new Eclipse instsnce freezes. I think I should do it in java threads, but unfortunatelly I don't know to use it correctly.

Please give me some ideas!


回答1:


Take a look the excellent article When Runtime.exec() won't from JavaWorld and see if it helps. In particular, this is probably your culprit:

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, and even deadlock.

The article provides various ways to address this problem, including the source code for a StreamGobbler class that consumes stderr and stdout in background threads.

It's amazing how well this article has held up. It was originally written in 2000 and I find just about all of it to still be accurate.



来源:https://stackoverflow.com/questions/2458520/java-process-inputstream-in-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!