Search code examples
javabatch-fileexecution

how to recognize batch file is executed or not in java?


I am trying to execute my batch file in java with the following code

String Extraction="cmd.exe /c start C:\\task\\Extracting.bat "; 
Runtime.getRuntime().exec(Extraction);

System.out.println("Extracted...");

Here, I want to execute the print statement after execution of the batch file. But if run the above code, first it opens the command prompt and runs the batch file, and it suddenly execute the print statement before batch file runs. help me to solve this problem.


Solution

  • One more thing that is worth a try is the following. It waits for execution

    String Extraction="cmd.exe /c start C:\\task\\Extracting.bat "; 
    //Runtime.getRuntime().exec(Extraction);
    
    final Process p = new ProcessBuilder("cmd.exe", "/c", "start /WAIT C:\\task\\Extracting.bat").redirectErrorStream(true).start();
    p.waitFor();
    System.out.println("Job done!!");