1

我想通过 java 会话运行命令。该命令包含空格。as "C:\With Space\sample.exe"-command_option"C:\Source File\test.c" 如果

C:\WithoutSpace\sample.exe -command_option "C:\Source File\test.c"

如果我们保留引号,C:\With Space\sample.exe我们会得到错误:“文件名、目录名或卷标语法不正确。” 如果我们删除引号,则 exe 不会运行...请指导。

谢谢,

4

2 回答 2

3

尝试这个:

String[] arg = {"cmd","/c","C:/Source File/test.c"};
ProcessBuilder pb = new ProcessBuilder(arg);
Process pr = pb.start();   

此外,您可以使用Runtime.exec(String[])版本

例子:

Runtime rt = Runtime.getRuntime();
String[] args = { "cmd", "/c", "C:/Source File/test.c"};
try
{
    Process proc = rt.exec(processCommand);
}
于 2013-04-25T12:28:34.137 回答
0

你可以试试这个:

Process process = Runtime.getRuntime().exec("'C:\WithoutSpace\sample.exe' -command_option 'C:\Source File\test.c'");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));

while ((line = in.readLine()) != null) {
    //line contain command output
}
于 2013-04-25T12:36:12.680 回答