【问题标题】:java.io.IOException: Cannot run program "dir": CreateProcess error=2, Das Systemjava.io.IOException: 无法运行程序 "dir": CreateProcess error=2, Das System
【发布时间】:2014-02-12 10:01:53
【问题描述】:

您好,我尝试在 eclipse 中运行以下 cmd 代码:

 "DIR \""+DEV_HOME+"\\src\"\\*.java /b /s >> \""+DEV_HOME+"\\bin\\javaFiles.txt\""

很明显它看起来像这样:

DIR "D:\Thomas\Dokumente\Daten\workspace\WBRLight\src"\*.java /b /s >> "D:\Thomas\Dokumente\Daten\workspace\WBRLight\bin\javaFiles.txt"

但我收到以下错误消息:

java.io.IOException: Cannot run program "dir": CreateProcess error=2, Das System kann die angegebene Datei nicht finden
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at java.lang.Runtime.exec(Runtime.java:617)
at java.lang.Runtime.exec(Runtime.java:450)
....

当我尝试使用 cmd 框中的代码时,它工作正常。 我的代码:

    public void run_cmdLine(String command) {
    try {
        Runtime rt = Runtime.getRuntime();
        BufferedReader input = null;
        Process pr = null;

        pr = rt.exec(command);
        input = new BufferedReader(new inputStreamReader(pr.getInputStream()));

        String line = null;

        while ((line = input.readLine()) != null) {
            System.out.println(line);
        }

        int exitVal = pr.waitFor();
        System.out.println("Exited with error code " + exitVal);

    } catch (Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: java eclipse cmd ioexception


    【解决方案1】:

    在命令字符串的开头添加"cmd.exe /c",应该可以解决问题。

    /c 参数将使cmd 完成并将其返回给 Java 进程。
    没有它,进程将挂起。

    【讨论】:

    • 当我这样做时,我没有得到任何回报。
    • 如果您使用 ProcessBuilder::start 而不是 Runtime::exec,则需要将字符串“cmd”、“/c”和“dir”分别传递给构造函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 2012-07-20
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2013-07-02
    • 2012-01-21
    相关资源
    最近更新 更多