wtf1989 wtf1989 - 25 days ago 6
Java Question

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

Answer

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:

enter image description here

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" };