简体   繁体   English

在程序文件名中使用带有空格的 Runtime.exec 时“无法运行程序”

[英]“Cannot run program” when using Runtime.exec with spaces in program filename

I am using the below code to open the "sample.html' file.我正在使用以下代码打开“sample.html”文件。

String filename = "C:/sample.html";

String browser = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe";

Runtime rTime = Runtime.getRuntime();

Process pc = rTime.exec(browser + filename);

pc.waitFor();

However, I am getting the below error.但是,我收到以下错误。

java.io.IOException: Cannot run program "C:/Program": CreateProcess error=2, The system cannot find the file specified

Could someone please help me figure this.有人可以帮我解决这个问题。 Thanks in advance.提前致谢。

Runtime.exec(String) automatically splits the string at spaces, assuming the first token is the command name and the rest are command line parameters. Runtime.exec(String)自动在空格处拆分字符串,假设第一个标记是命令名称,其余是命令行参数。 Also you do not have a space between browser and file , although that is not the root cause of the problem.此外,您在browserfile之间没有空格,尽管这不是问题的根本原因。

It thinks you want to run "C:/Program" with two command line arguments:它认为你想用两个命令行参数运行“C:/Program”:

  1. "Files" “文件”
  2. "(x86)/google/Chrome/Application/chrome.exeC:/sample.html" “(x86)/google/Chrome/Application/chrome.exeC:/sample.html”

Use Runtime.exec(String[]) instead, that way you have full control over what is what:改用Runtime.exec(String[]) ,这样你就可以完全控制什么是什么:

 String[] command = new String[]{browser, filename};
 Runtime.exec(command);

Try this.尝试这个。

    String filename = "C:\\sample.html";
    String browser = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";

    Runtime runtime = Runtime.getRuntime();

    try {
        runtime.exec(new String[] {browser, filename});
    } catch (IOException e) {
        e.printStackTrace();
    }

Stop using Runtime.exec(String) - the problem is in how it processes the single string input.停止使用Runtime.exec(String) - 问题在于它如何处理单个字符串输入。

The error message indicates how/where it is failing: note that it stops after "C:/Program" (or, the first space).错误消息指示失败的方式/位置:请注意,它在“C:/Program”(或第一个空格)之后停止。 This indicates that exec parsed the string "incorrectly" and thus isn't even looking for the correct executable.这表明 exec “错误地”解析了字符串,因此甚至没有寻找正确的可执行文件。

Cannot run program "C:/Program"无法运行程序“C:/Program”

Instead, consider the use of ProcessBuilder .相反,请考虑使用ProcessBuilder While the usage is still system-dependent, ProcessBuilder allows separation of the executable file-name (and need to deal with it specially) and the arguments and does it's darnedest to invoke the target correctly.虽然用法仍然依赖于系统,但 ProcessBuilder 允许将可执行文件名(并且需要专门处理它)和参数分开,并且正确调用目标是最好的。

String filename = "C:\\sample.html";
String browser = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";

ProcessBuilder pb = new ProcessBuilder(browser, filename);
// setup other options ..
// .. and run
Process p = pb.start();
p.waitFor();

From what I can tell, in Windows, ProcessBuilder will wraps the individual components in quotes;据我所知,在 Windows 中,ProcessBuilder 会将各个组件用引号括起来; this can create a different problem when arguments contain quotes..当参数包含引号时,这可能会产生不同的问题..

Parameters must be passed separately:参数必须单独传递:

Process pc = rTime.exec(new String[]{browser, filename});

Using exec() is not like using the command line - you can not use spaces to delimit the command from its parameters.使用exec()与使用命令行不同 - 您不能使用空格将命令与其参数分隔开。 Your attempt would try to execute a command whose path was the concatenation of the exec and the filename as one giant string.您的尝试会尝试执行一个命令,其路径是 exec 和文件名的串联,作为一个巨大的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM