我使用下面的代码打开“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();
但是,我收到以下错误。
java.io.IOException: Cannot run program "C:/Program": CreateProcess error=2, The system cannot find the file specified
有人可以帮我解决这个问题吗?提前致谢。
Runtime.exec(String)
自动以空格分割字符串,假设第一个标记是命令名称,其余是命令行参数。而且你之间没有空格browser
and file
,尽管这不是问题的根本原因。
它认为您想使用两个命令行参数运行“C:/Program”:
- "Files"
- “(x86)/google/Chrome/Application/chrome.exeC:/sample.html”
Use Runtime.exec(String[]) http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec%28java.lang.String%5B%5D%29相反,这样你就可以完全控制什么是什么:
String[] command = new String[]{browser, filename};
Runtime.exec(command);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)