开发者

Why does Process.waitFor() never return?

I am launching a windows process (wrote in C++ but I don't have sources) from Java code in the following way:

 Process p1 = Runtime.getRuntime().exec(cmdAndParams);
 p1.waitFor();

My problem is that the waitFor() method never ends. Thus I tried to launch the process in a simple shell and it ends correctly with many print in the shell (standard output I guess).

Thus I decided to create and start a thread reading t开发者_运维知识库he standard output even if I don't need for now these outputs. This fixed the issue.

So my question is the following one: Is this solution the "Java standard to launch and wait for external processes having outputs" or does it means there is an issue somewhere in the native process ? If such an issue exist what C++ programming "error" can be at the origin of the issue ?


This is an OS thing. The child process is writing to stdout, and that's being buffered waiting for your Java process to read it. When you don't read it, the buffer eventually fills up and the child process blocks writing to stdout waiting for buffer space.

You would have to processes the child process' stdout (and stderr) whichever language you were using.

I suggest you read this article (all 4 pages of it) and implement the recommendations there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜