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();
Syntax TIMEOUT delay [/nobreak] Key delay Delay in seconds (between -1 and 100000) to wait before continuing. The value -1 causes the computer to wait indefinitely for a keystroke (like the PAUSE command) /nobreak Ignore user key strokes.
Use /WAIT to Wait for a Command to Finish Execution When we start a program in a Batch file using the START command, we can wait until the program is finished by adding /wait to the START command. Even if there are multiple commands, /wait can be used for each process to finish and move to the next one.
Pause syntaxSuspends processing of a batch program and displays the message: Press any key to continue.
Type the "cmd /k" parameter before every command to keep the window from closing.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With