4

次のコードがあります:

Process p = Runtime.getRuntime().exec(args);

プログラムでRuntime.getRuntime()。exec(args);を待機します。終了するには、2〜3秒続けてから続行します。

アイデア?

4

2 に答える 2

7

Process.waitFor()を使用します:

Process p = Runtime.getRuntime().exec(args);
int status = p.waitFor();

JavaDocから:

このProcessオブジェクトによって表されるプロセスが終了するまで、必要に応じて現在のスレッドを待機させます。サブプロセスがすでに終了している場合、このメソッドはすぐに戻ります。サブプロセスがまだ終了していない場合、呼び出し元のスレッドはサブプロセスが終了するまでブロックされます。

于 2010-04-01T09:40:46.597 に答える
2

サンプルコードは次のとおりです。

Process proc = Runtime.getRuntime().exec(ANonJava.exe@);
InputStream in = proc.getInputStream();
byte buff[] = new byte[1024];
int cbRead;

try {
    while ((cbRead = in.read(buff)) != -1) {
        // Use the output of the process...
    }
} catch (IOException e) {
    // Insert code to handle exceptions that occur
    // when reading the process output
}

// No more output was available from the process, so...

// Ensure that the process completes
try {
    proc.waitFor();
} catch (InterruptedException) {
    // Handle exception that could occur when waiting
    // for a spawned process to terminate
}

// Then examine the process exit code
if (proc.exitValue() == 1) {
    // Use the exit value...
}

詳細については、次のサイトをご覧ください:http: //docs.rinet.ru/JWP/ch14.htm

于 2010-04-01T09:42:16.920 に答える