use*_*937 4 java windows process
到目前为止我有这个:
public static void main(String[] args) {
try {
String line;
Process p = Runtime.getRuntime().exec(
System.getenv("windir") + "\\system32\\" + "tasklist.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line); // <-- Parse data here.
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
Scanner killer = new Scanner(System.in);
int tokill;
System.out.println("Enter PID to be killed: ");
tokill = killer.nextInt();
}
Run Code Online (Sandbox Code Playgroud)
}
我希望能够根据用户输入的PID终止进程.我怎样才能做到这一点?(只需要在Windows上工作).*注意:必须能够杀死任何进程,inc.SYSTEM进程,所以如果使用taskkill.exe执行此操作,我猜测将需要-F标志?
所以,如果我有
Runtime.getRuntime().exec("taskkill /F /PID 827");
Run Code Online (Sandbox Code Playgroud)
如何用我的tokill变量替换"827"?
只需构建字符串即可终止进程:
String cmd = "taskkill /F /PID " + tokill;
Runtime.getRuntime().exec(cmd);
Run Code Online (Sandbox Code Playgroud)
我现在不坐在 Windows 计算机前。但如果tasklist适合您,您可以使用ProcessBuilder来运行 Windows 命令taskkill。使用实例进行taskkill这样的调用(将 %pid% 替换为实际的 pid)。您不需要两个可执行文件的绝对路径,因为它位于路径变量中。ProcessBuildercmd /c taskkill /pid %pid%c:/windows/system32
正如埃里克(在对你的问题的评论中)指出的那样,很多人以前都有过这个答案。