2016-11-15 48 views
1

如何修改這些代碼,使其工作:的Java文件路徑和URL

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

第一個「過程」的工作很好,但「進程2」開默認站點在IE而非google.com

回答

0

VAR1是錯誤的,你有一個scaped 在開始

它必須是:

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

是異常的原因

現在根據DOC:

enter image description here

你需要在同一陣列中通過執行命令和參數...

所以它必須是唯一的

Process process2 = runTime.exec(var1); 

,其中

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

以前試過,還是默認的IE瀏覽器的網站,而不是google.com – wtf1989

+0

工作:)謝謝,我敢肯定,我嘗試過,但沒有逗號 – wtf1989

0

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


process使用方法:

public Process exec(String[] cmdarray) throws IOException 

執行在一個單獨過程中的指定的命令和參數。 這是一種方便的方法。調用exec(cmdarray) 的行爲與調用exec(cmdarray,null, null)的行爲完全相同。


process2使用方法:

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

執行在一個單獨的過程 指定環境指定的命令和參數。這是一種方便的方法。一個 調用exec(cmdarray,envp)的行爲與調用exec(cmdarray,envp,null)的行爲方式完全相同 。


嘗試這樣做:

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

謝謝,但不工作:) – wtf1989