在java中确定使用Runtime环境创建的进程是否已完成执行? [英] In java determine if a process created using Runtime environment has finished execution?

查看:112
本文介绍了在java中确定使用Runtime环境创建的进程是否已完成执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Runtime.getRuntime.exex("abc.exe -parameters");

使用 .waitFor()无效确定流程的完成情况。

using .waitFor() does not help to determine the completion of process.

推荐答案

Process.waitFor() 应该工作。如果它不起作用那么:

Process.waitFor() should work. If it doesn't work then either:


  • JVM中存在一个错误(这种情况不太可能发生),或者

  • there's a bug in the JVM (highly unlikely for something like this), or

关于流程和/或Java代码的某些内容意味着流程不会退出。

there is something about the process and/or your Java code that means that the process won't exit.

从Java启动的进程不会/不能退出的最可能原因是:

The most likely reasons that a process launched from Java won't / can't exit are:


  • 该进程被阻止等待您的Java应用程序给它一些输入,

  • the process is blocked waiting for your Java application to give it some input,

进程被阻止等待Java应用程序读取其输出,

the process is blocked waiting for your Java application to read its output,

它被阻塞等待某些外部事件;例如如果它试图说话没有响应的远程服务器。

it is blocked waiting on some external event; e.g. if it is trying to talk remote server that is not responding.

有人发送了一个STOP信号,或者

someone has sent it a STOP signal, or

这只是花了很长时间才能运行。

it is just taking a looong time to run.

前两个原因/原因可以通过(分别)关闭连接到其标准输入的Java输出流,以及读取(并可能丢弃)连接到其标准输出和标准错误的Java输入流来解决。其他原因是棘手的,你唯一的选择就是等待它或者如果花费的时间太长就试图杀掉这个过程。

The first two of these reasons / causes can be addressed by (respectively) closing the Java output stream connected to its standard input, and reading (and possibly discarding) the Java input streams connected to its standard output and standard error. The other causes are intractable, and your only options are to wait it out or attempt to kill off the process if it takes too long.

底线 - 找出您的流程未完成的原因。被阻止的 Process.waitFor()是症状,而不是疾病。

Bottom line - find out why your process isn't completing. The blocked Process.waitFor() is the symptom, not the disease.

这篇关于在java中确定使用Runtime环境创建的进程是否已完成执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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