Guy Ben-Shahar
Guy Ben-Shahar

Reputation: 231

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?

Upvotes: 3

Views: 464

Answers (2)

Guy Ben-Shahar
Guy Ben-Shahar

Reputation: 231

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

Upvotes: 2

nano_nano
nano_nano

Reputation: 12523

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

Upvotes: 1

Related Questions