2013-12-14 56 views
1

在下面的代碼中,我試圖殺死正在運行的iTunes進程。但我得到例外。我錯過了什麼?嘗試殺死任務時發生異常

代碼:

import java.io.IOException; 

public class KillProcess { 

    public static void main(String[] args) throws IOException { 
     Runtime.getRuntime().exec("TASK KILL /F /IM itunes.exe"); 
    } 

} 

例外:

Exception in thread "main" java.io.IOException: Cannot run program "TASK": CreateProcess error=2, The system cannot find the file specified 
    at java.lang.ProcessBuilder.start(Unknown Source) 
    at java.lang.Runtime.exec(Unknown Source) 
    at java.lang.Runtime.exec(Unknown Source) 

回答

3

應該有TASKKILL之間沒有空格。它是TASKKILL

Runtime.getRuntime().exec("TASKKILL /F /IM itunes.exe"); 
2
ProcessBuilder pb = new ProcessBuilder("taskkill","/F","IM","itunes.exe"); 
pb = pb.redirectErrorStream(true); 
Process proc = pb.start(); 

這也應該工作。

相關問題