如何等待批处理命令在java中完成其执行

时间:2013-05-02 07:48:32

标签: java process batch-processing runtime.exec

我想等到我的批处理命令完成执行并创建一个csv文件作为输出。代码如下所示批处理文件

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

然后我想使用CSV文件(app.csv)来读取它的内容。但是,当我运行程序时,我变得喜欢。

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)

如何解决此问题

2 个答案:

答案 0 :(得分:1)

尝试pr.waitFor此方法将返回退出代码,除了0以外的任何进程都失败。

Ins String command = "cmd /c start " + batFile;使用String command = "cmd /c " + batFile

答案 1 :(得分:0)

Runtime.exec会返回Process对象(pr,在上面的示例中)。只需调用pr.waitFor()等待进程退出。

相关问题