gpt4 book ai didi

java - 只有在通过 java 完成进程后,我的代码才会继续

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:36 24 4
gpt4 key购买 nike

我正在通过一个进程运行 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");

关于java - 只有在通过 java 完成进程后,我的代码才会继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34175565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com