为什么Process.waitFor()永远不会返回? [英] Why does Process.waitFor() never return?

查看:3083
本文介绍了为什么Process.waitFor()永远不会返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方式从Java代码启动一个Windows进程(用C ++编写,但没有源代码):

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();

我的问题是waitFor()方法永远不会结束。因此,我试图在一个简单的shell启动进程,它正确地结束了许多打印在shell(标准输出我猜)。

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 the standard output even if I don't need for now these outputs. This fixed the issue.

所以我的问题是下面这个解决方案:Java标准启动并等待外部进程有输出,或者这意味着是本地进程中的某个地方的问题?如果这样的问题存在什么C ++编程错误可以是问题的起源?

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 ?

推荐答案

这是一个操作系统的事情。子进程正在写入stdout,并且正在缓冲等待您的Java进程读取它。当你不读它,缓冲区最终填满,子进程阻塞写入stdout等待缓冲区空间。

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.

你必须处理子进程'

我建议你阅读这篇文章(全部4页),并在其中实施建议。

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

这篇关于为什么Process.waitFor()永远不会返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆