尝试杀死任务时出现异常

时间:2013-12-14 11:47:05

标签: java

在下面的代码中,我试图杀死正在运行的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)

2 个答案:

答案 0 :(得分:3)

TASKKILL之间不应有空格。它是TASKKILL

Runtime.getRuntime().exec("TASKKILL /F /IM itunes.exe");

答案 1 :(得分:2)

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

这也应该有用。