简体   繁体   中英

Java file path and url in Process class

How to edit these code to make it work:

 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);

first "process" work fine, but "process2" open default site in IE instead of google.com

the var1 is wrong you have a scaped " at the begin

it must be:

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

that is the reason of the exception

Now according to the doc:

在此输入图像描述

you need to pass in the same array the command to execute and the parameters...

so it must be only

Process process2 = runTime.exec(var1);

where

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 is using the method:

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 is using the method:

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).


Try doing:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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