从Java运行.bat / .cmd文件

时间:2010-07-18 10:32:55

标签: java

我想从Java运行.cmd文件。我有一些适合我的东西。有人可以帮助我理解我的程序可能的失败。

import java.io.IOException;  

/* 
  How to run a batch .bat or .cmd file from Java? 
  1. I don't want the command window to open up. It should be in background. 
  2. Gracefully destroy any new process created. 
  3. Need to confirm the quality of the program with experts. 
 */  
public class RunBat {  
    public static void main(String args[]) {  
        Runtime run = Runtime.getRuntime();  
        //The best possible I found is to construct a command which you want to execute  
        //as a string and use that in exec. If the batch file takes command line arguments  
        //the command can be constructed a array of strings and pass the array as input to  
        //the exec method. The command can also be passed externally as input to the method.  

        Process p = null;  
        String cmd = "D:\\Database\\TableToCSV.cmd";  
        try {  
            p = run.exec(cmd);  
            p.getErrorStream();  
            System.out.println("RUN.COMPLETED.SUCCESSFULLY");  
        }  
        catch (IOException e) {  
            e.printStackTrace();  
            System.out.println("ERROR.RUNNING.CMD");  
            p.destroy();  
        }  
    }  
}  

我的解决方案可靠吗?我怎样才能确保一旦执行.cmd就没有进程挂起。

感谢。

4 个答案:

答案 0 :(得分:5)

我不知道你在用p.getErrorStream()做什么,你没有访问它。

确定结果的方法,即执行的命令的退出代码是在

之后添加以下行
p = run.exec(cmd);
p.waitFor();
System.out.println(p.exitValue());

将p.destroy()放入finally块中。

希望这有帮助。

答案 1 :(得分:3)

执行命令:

cmd.exe /C d:\database\tabletoCSV.cmd

有关详细信息,请参阅cmd.exe /?

> cmd /?
Starts a new instance of the Windows command interpreter

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
   [[/S] [/C | /K] string]

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains
[...]

答案 2 :(得分:1)

像刚才提到的卡尔一样

  • 您没有捕获任何输出错误/成功输出。
  • 您没有让进程线程等待exitValue。
  • 您是否看过ProcessBuilder类?

无论如何,您可以查看以下代码

    Process proc = null;
    Runtime rt = Runtime.getRuntime();
    try {
        proc = rt.exec(cmd);
        InputStream outCmdStream = proc.getInputStream();
        InputStreamReader outCmdReader = new InputStreamReader(outCmdStream);
        BufferedReader outCmdBufReader = new BufferedReader(outCmdReader);
        String outLine;
        while ((outLine = outCmdBufReader.readLine()) != null) {
            System.out.println(outLine);
        }
        InputStream errStream = proc.getErrorStream();
        InputStreamReader errReader = new InputStreamReader(errStream);
        BufferedReader errBufReader = new BufferedReader(errReader);
        String errLine;
        while ((errLine = errBufReader.readLine()) != null) {
            System.out.println(errLine);
        }
        int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);

    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("ERROR.RUNNING.CMD");
        proc.destroy();
    }
}

希望这有帮助

答案 3 :(得分:0)

此代码还有另一个问题,其他答案并未指出:如果您启动的进程生成(控制台)输出并且您没有连接其输出流,它将停止并失败,没有明显的原因。对于某些程序和环境,我发现有必要连接单独的线程以保持输出和错误流的消耗。并且要捕捉他们的输出,这样你就不会失明。

如果你有一个现代Java(第1.5版),你也可以将ProcessBuilder类作为启动外部程序的一种方法。