0

我正在尝试使用带有以下代码的 JDK7u25 运行 cmd 文件:

try {
            ProcessBuilder pb = new ProcessBuilder(cmd);
            pb.directory(workingDir);
            proc = pb.start();
        } catch (IOException e) {
            System.out.println(e.getMessage());
            throw e;
        }
        // StdOut and Err Stream must be read immediatly even if they are not used

        // any error message?
        StreamInlet error = new StreamInlet(proc.getErrorStream(), "ERROR");

        // any output?
        StreamInlet output = new StreamInlet(proc.getInputStream(), "OUTPUT");

        // kick them off
        error.start();
        output.start();

        if (wait) {
            try {
                exitCode = proc.waitFor();
            } catch (InterruptedException e) {
                System.out.println("Waiting for process was interrupted");
            }
            if (addMetaInfo)
                System.out.println("Return value = " + exitCode);
        }

在哪里cmd=[cmd.exe, /c, C:\My Root\scripts\windows\tools\MyCLI.cmd, -c, C:\Local Disk D\My Tutorial\RegressionTests.xml, -d, 02_RecordViewer_Test, -l"ERROR"]

但它不起作用,我得到以下输出。

'C:\My' 未被识别为内部或外部命令,

可运行的程序或批处理文件。

我已经通过在调用 cmd 文件之前添加显式“CMD.EXE /C”对JDK7U21 问题进行了必要的更改。我也在使用 JDK7u21 问题中提到的 ProcessBuilder 类。

如果我尝试执行的 cmd 文件放在 C:\MyRoot 中,即名称中没有空格的文件夹,它工作正常。

有人可以帮忙吗?

4

3 回答 3

3

您需要根据需要将路径用引号引起来cmd

String[] cmd = {"cmd.exe", "/c", "\"C:\\My Root\\scripts\\windows\\tools\\MyCLI.cmd\"", "-c", "\"C:\\Local Disk D\\My Tutorial\\RegressionTests.xml\"",.....};

正如我们通过聊天讨论的那样更新 ,问题似乎在于ProcessBuilder将参数传递给cmd.exe. 但是由于您拥有可执行文件的完整路径,因此实际上cmd.exe根本不需要。所以命令看起来像这样:

String[] cmd = {"\"C:\\My Root\\scripts\\windows\\tools\\MyCLI.cmd\"", "-c", "\"C:\\Local Disk D\\My Tutorial\\RegressionTests.xml\"",.....};
于 2013-08-16T12:18:57.680 回答
1

我刚刚注意到这个问题已经在 J​​DK7u25 中得到解决。我刚刚在它的Release Notes中找到了它。

于 2013-08-19T17:36:42.477 回答
0

我很想将反斜杠更改为正斜杠,并转义空格。

C:/My\ Root/scripts/windows/tools/MyCLI.cmd
于 2013-08-16T12:21:52.373 回答