简体   繁体   中英

My code will continue only after process is finished via java

I am running an exec command through a process. 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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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