javaprocessbatch-processingruntime.exec

how to wait for batch command to complete its excecution in java


I want to wait till my batch command completes its execution and creates a CSV file as output. The code is as bellow for executing batch file

String command = "cmd /c start " + batFile;
        Runtime rt = Runtime.getRuntime();
        Process pr = rt.exec(command);

Then I want to use the CSV file(app.csv) to read the contents of it. But when I run the program, I am getting like.

java.io.FileNotFoundException: C:\appanalysis\app.csv (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:120)
    at java.io.FileInputStream.<init>(FileInputStream.java:79)
    at java.io.FileReader.<init>(FileReader.java:41)

How to resolve this?


Solution

  • Try pr.waitFor this method will return the exit code, anything other than 0 the process failed.

    Insted of String command = "cmd /c start " + batFile; use String command = "cmd /c " + batFile