简体   繁体   中英

how can check Runtime.exec(cmd) is completed or not

how can check Runtime.exec(cmd) is completed or not?

It is running but how can I check command is executed or not in java program?

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?

The Process instance can help you manipulate the execution of your runtime through its InputStream and Outpustream.

The process instance has a waitFor() method that allows the current Thread to wait for the termination of the subprocess.

To control the completion of your process, you can also get the exitValue of your runtime, but this depends on what the process returns.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM