在下面的代码中,我试图杀死正在运行的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)
答案 0 :(得分:3)
TASK
和KILL
之间不应有空格。它是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();
这也应该有用。