Java 文件路径和进程 class 中的 url

Java file path and url in Process class

如何编辑这些代码以使其工作:

 String[] var1 = { "\"C:\Program Files\Internet Explorer\iexplore.exe" };
 String[] var2 = { "http://google.com" };

 Runtime runTime = Runtime.getRuntime();
 Process process = runTime.exec("\"C:\Program Files\Internet Explorer\iexplore.exe\" http://google.com");
 Process process2 = runTime.exec(var1,var2);

首先 "process" 工作正常,但是 "process2" 在 IE 中打开默认站点而不是 google.com

var1 是错误的,你在开头有一个 scaped "

必须是:

String[] var1 = { "C:\Program Files\Internet Explorer\iexplore.exe" };

这就是异常的原因

现在根据文档:

你需要在同一个数组中传递要执行的命令和参数...

所以一定只有

Process process2 = runTime.exec(var1);

其中

String[] var1 = { "C:\Program Files\Internet Explorer\iexplore.exe", "http://google.com" };

https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html


process 正在使用方法:

public Process exec(String[] cmdarray) throws IOException

Executes the specified command and arguments in a separate process. This is a convenience method. An invocation of the form exec(cmdarray) behaves in exactly the same way as the invocation exec(cmdarray, null, null).


process2 正在使用方法:

public Process exec(String[] cmdarray, String[] envp) throws IOException

Executes the specified command and arguments in a separate process with the specified environment. This is a convenience method. An invocation of the form exec(cmdarray, envp) behaves in exactly the same way as the invocation exec(cmdarray, envp, null).


尝试做:

String var1 = "\"C:\Program Files\Internet Explorer\iexplore.exe";
String var2 = "http://google.com";
Process process2 = runTime.exec({var1,var2});