简体   繁体   English

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

[英]My code will continue only after process is finished via java

I am running an exec command through a process. 我正在通过一个进程运行exec命令。 I want that my program will keep running only after the process is finished. 我希望我的程序只在进程完成后才会继续运行。 This is my code: 这是我的代码:

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");

I need that the rest of the code will be executed only after the process is done, because it's depending on the process output. 我需要其余的代码只在进程完成后执行,因为它取决于进程输出。 Any ideas? 有任何想法吗?

I've got the answer and now it works!!! 我有答案,现在它有效!

Eevery process has input and output stream. Eevery流程具有输入和输出流。 my specific process had to empty his input buffer to continue running. 我的具体过程必须清空他的输入缓冲区才能继续运行。 All i added is the following code: 我添加的是以下代码:

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");

waitFor is for this purpose: 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.");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM