У меня есть следующий код:
Process p = Runtime.getRuntime().exec(args);
и я хочу, чтобы моя программа ожидала Runtime.getRuntime (). Exec (args); чтобы закончить, это продлится 2-3 секунды, а затем продолжите.
Идеи
использовать Process.waitFor () :
Process p = Runtime.getRuntime().exec(args); int status = p.waitFor();
Из JavaDoc:
заставляет текущий поток при необходимости ждать, пока процесс, представленный этим объектом Process, не будет завершен. Этот метод сразу возвращается, если подпроцесс уже завершен. Если подпроцесс еще не завершен, вызывающий поток будет заблокирован до выхода из подпроцесса.
Вот пример кода:
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