我正在通过进程运行exec命令。我希望我的程序只在进程完成后才会继续运行。这是我的代码:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("tools/jadx/bin/jadx.bat -d JavaProjects/");
//the rest of the code
System.out.println("Process is finished");
我需要在完成进程后才执行其余的代码,因为它取决于进程输出。有什么想法吗?
答案 0 :(得分:2)
我已经得到了答案,现在可行了!!!
每个进程都有输入和输出流。我的具体过程必须清空他的输入缓冲区才能继续运行。我添加的是以下代码:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("tools/jadx/bin/jadx.bat -d JavaProjects/");
out = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
int lExitCode = process.waitFor();
if (lExitCode == 0)
System.out.println("\n\n$$$$$ Process was finished successfully $$$$$\n\n");
else
System.out.println("\n\n$$$$$ Process was finished not successfully $$$$$\n\n");
答案 1 :(得分:1)
waitFor
就是为了这个目的:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("tools/jadx/bin/jadx.bat -d JavaProjects/");
int lExitCode = process.waitFor();
//the rest of the code
if (lExitCode == 0)
System.out.println("Process was finished successfull.");
else
System.out.println("Process was finished not successfull.");