我正在通过进程运行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");
我需要其余的代码仅在处理完成后才执行,因为它取决于处理输出.有任何想法吗?
解决方法:
我有答案,现在可以了!!!
每个过程都有输入和输出流.我的特定过程必须清空他的输入缓冲区才能继续运行.我添加的只是以下代码:
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");
标签:multithreading,runtime,java,process
来源: https://codeday.me/bug/20191119/2035321.html