2014-01-30 159 views
1

我必須在Mac OS執行命令:執行killall命令從Java

killall -KILL "Google Chrome" 

當我在終端執行它或使用以下命令運行它的工作原理.command文件。

我想這一切在Java代碼中

Runtime.getRuntime().exec("/usr/bin/killall -KILL \"Google Chrome\""); 
Runtime.getRuntime().exec("killall -KILL \"Google Chrome\""); 
Runtime.getRuntime().exec("/bin/bash -c \"killall -KILL \\\"Google Chrome\\\"\""); 
Runtime.getRuntime().exec("bash -c \"killall -KILL \\\"Google Chrome\\\"\""); 
Runtime.getRuntime().exec("/usr/bin/killall", new String[]{"-KILL", "Google Chrome"}); 
Runtime.getRuntime().exec("killall", new String[]{"-KILL", "Google Chrome"}); 
Runtime.getRuntime().exec("/bin/bash", new String[]{"-c", "killall -KILL \"Google Chrome\""}); 
Runtime.getRuntime().exec("bash", new String[]{"-c", "killall -KILL \"Google Chrome\""}); 

而且這是行不通的。

可能是什麼問題?

回答

1

你在這裏。

String cmds[] = {"killall","Google Chrome"}; 
Runtime.getRuntime().exec(cmds); 
+0

http://docs.oracle.com /javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String []) – SnakeDoc

+0

是的,沒錯。我用exec(String)和exec(String,String [])使用了錯誤的變體,完全錯過了正確的一個:exec(String [])。謝謝! – user2780836

+0

@ user2780836如果這是您最喜歡的正確答案,請隨時用綠色複選標記標記。 – Rainbolt

1

你可以試試這個:

String[] command = { "/usr/bin/killall", "-KILL", "Google Chrome" }; 

Runtime.getRuntime().exec(command); 

您也可以嘗試上面,通過添加引號谷歌瀏覽器如下:

String[] command = { "/usr/bin/killall", "-KILL","\"Google Chrome\"" }; 
+0

是的,沒錯。我用exec(String)和exec(String,String [])使用了錯誤的變體,完全錯過了正確的一個:exec(String [])。謝謝! – user2780836