如何检查Runtime.exec(cmd)是否完成 [英] how can check Runtime.exec(cmd) is completed or not
问题描述
如何检查Runtime.exec(cmd)是否已完成?
how can check Runtime.exec(cmd) is completed or not?
它正在运行但是如何在java程序中检查命令是否执行?
It is running but how can I check command is executed or not in java program?
如果我使用进程检查那么它会阻塞当前线程。如果我停止运行程序,则执行Runtime.exec(cmd)。我该怎么办?
If I used process to check then it is blocking current thread. If I stopped running program then Runtime.exec(cmd) is executing. What should I do?
推荐答案
进程
实例可以帮助您通过其InputStream操作运行时的执行Outpustream。
The Process
instance can help you manipulate the execution of your runtime through its InputStream and Outpustream.
流程实例有 waitFor()
允许当前 Thread $ c $的方法c>等待子进程终止。
The process instance has a waitFor()
method that allows the current Thread
to wait for the termination of the subprocess.
要控制进程的完成,你还可以得到 exitValue
你的运行时,但这取决于进程返回的内容。
To control the completion of your process, you can also get the exitValue
of your runtime, but this depends on what the process returns.
这是一个如何使用 waitFor的简单示例
并获取流程结果(请注意,这将阻止您当前的线程,直到流程结束)。
Here is a simple example of how to use the waitFor
and get the process result (Note that this will block your current Thread until the process has ended).
public int runCommand(String command) throws IOException
{
int returnValue = -1;
try {
Process process = Runtime.getRuntime().exec( command );
process.waitFor();
returnValue = process.exitValue();
} catch (InterruptedException e) {
e.printStackTrace();
}
return returnValue;
}
这篇关于如何检查Runtime.exec(cmd)是否完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!