Search code examples
javaprocessbuilder

How do I get the information of a batch script started in java, whether it has already ended


im calling multiple batch files in Java with the ProcessBuilder like

ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/c", "Start", "batchTest.bat"); 
File dir = new File(path);
processBuilder.directory(dir);
try {
    Process p = pb.start();
} catch (IOException e) {
// TODO Auto-generated catch block
    e.printStackTrace();
}

Now i want the information that the script is done in java. Like just a sysout when the script is completly finished. process.isAlive() turns false before the script is done. And also process.witFor() doesnt get the job done. Maybe someone has an idea or a workaround?


Solution

  • Okay, i got it. process.waitFor() only works if you add the parameter "/wait" to the Constructor of the ProcessBuilder.

    new ProcessBuilder("cmd", "/c", "Start", "/wait", "batchTest.bat");