Reputation: 51
Need to wait for the cmd command to get executed before running the display function.
Need the Process p
to get executed completely before executing display function.
String command1 = "cmd /c start cmd.exe /k \"" + processCommand1 + " && " + processCommand2 +" && "+ exitCommand+"\"";
Process p = Runtime.getRuntime().exec(command1);
display();
Upvotes: 5
Views: 269
Reputation: 510
Better to use java.lang.ProcessBuilder. JavaDoc had example: https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html Something like that:
ProcessBuilder processBuilder = new ProcessBuilder("...");
...
Process process = processBuilder.start();
...
int exitCode = process.waitFor();
Upvotes: 1
Reputation: 44970
One way would be to use Process.waitFor()
. However in your example you are using cmd /c start
which will run the actual program asynchronously in the background. You should ensure that the program is started synchronously without start
so you can wait for it.
Upvotes: 8
Reputation: 21
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
output.append(line + "\n");
if (line.contains(exitConfirm)) {
break;
}
}
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
display()
this code will either wait for a specific exit line or wait until the process is completed
Upvotes: -2