在 Java 中杀死进程的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35230507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:26:36  来源:igfitidea点击:

The right way to kill a process in Java

javaprocesskillruntime.execdestroy

提问by Kariamoss

What's the best way to kill a process in Java ?

在 Java 中杀死进程的最佳方法是什么?

Get the PID and then killing it with Runtime.exec()?

获取 PID 然后用Runtime.exec()?

Use destroyForcibly()?

使用destroyForcibly()?

What's the difference between these two methods, and is there any others solutions ?

这两种方法有什么区别,还有其他解决方案吗?

采纳答案by Derlin

If the process you want to kill has been started by your application

如果您要杀死的进程已被您的应用程序启动

Then you probably have a reference to it (ProcessBuilder.start()or Runtime.exec()both return a reference). In this case, you can simply call p.destroy(). I think this is the cleanest way (but be careful: sub-processes started by pmay stay alive, check http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4770092for more info).

那么你可能有一个对它的引用(ProcessBuilder.start()Runtime.exec()两者都返回一个引用)。在这种情况下,您只需调用p.destroy(). 我认为这是最干净的方式(但要小心:由 启动的子进程p可能会保持活动状态,查看http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4770092了解更多信息)。

The destroyForciblyshould only be used if destroy()failed after a certain timeout. In a nutshell

destroyForcibly是否应该只用于destroy()故障一定超时后。简而言之

  1. terminate process with destroy()
  2. allow process to exit gracefully with reasonable timeout
  3. kill it with destroyForcibly()if process is still alive
  1. 终止进程 destroy()
  2. 允许进程以合理的超时正常退出
  3. destroyForcibly()如果进程还活着,就杀死它


If the process you want to kill is external

如果你想杀死的进程是外部的

Then you don't have much choice: you need to pass through the OS API (Runtime.exec). On Windows, the program to call will be taskkill.exe, while on Mac and Linux you can try kill.

那么你就没有太多选择:你需要通过 OS API ( Runtime.exec)。在 Windows 上,要调用的程序将是taskkill.exe,而在 Mac 和 Linux 上,您可以尝试kill.



Have a look at https://github.com/zeroturnaround/zt-exec/issues/19and Killing a process using Javaand http://invisiblecomputer.wonderhowto.com/how-to/code-simple-java-app-kill-any-process-after-specified-time-0133513/for more info.

看看https://github.com/zeroturnaround/zt-exec/issues/19Killing a process using Javahttp://invisiblecomputer.wonderhowto.com/how-to/code-simple-java-app- kill-any-process-after-specified-time-0133513/了解更多信息。

回答by nlloyd

If you're trying to kill the main process your java code started, I'd suggest using System.exit(). The benefits are explained here: when should we call system exit in java.

如果您试图终止 Java 代码启动的主进程,我建议您使用System.exit(). 这里解释了好处:我们什么时候应该在 java 中调用 system exit

Essentially, System.exit()will run shutdown hooks that will make sure any dependent non-daemon processes that may not have completed their work are killed before your process is killed. This is the clean way to do it.

本质上,System.exit()将运行关闭挂钩,以确保在您的进程被终止之前,任何可能尚未完成其工作的相关非守护进程被终止。这是做到这一点的干净方式。

If the process is not yours, you will have to rely on the Operating System to do this work for you as explained in this answer: Killing a Process Using Java

如果该进程不是您的,则您将不得不依赖操作系统为您完成这项工作,如本答案中所述:使用 Java 杀死进程

In that case your suggestion of Runtime.exec()a kill on *nix would be a decent way to go.

在这种情况下,您建议Runtime.exec()杀死 *nix 将是一个不错的选择。

Now as for destroyForcibly(), you're typically going to call that on a child process spawned by your java code that was presumably started with the process api's ProcessBuilder.start()or Runtime.exec()

现在,对于destroyForcibly(),您通常会在由您的 java 代码产生的子进程上调用它,该子进程大概是由进程 apiProcessBuilder.start()Runtime.exec()

回答by Meir Bookatz

the one that worked for me is System.exit(0); it's work's well because it closes all still running processes and components

对我有用的是 System.exit(0); 它运行良好,因为它关闭了所有仍在运行的进程和组件

回答by u7514037

In java 8 source code

在 java 8 源代码中

public Process destroyForcibly() {
    destroy();
    return this;
}

I just want to say that the destroyForciblyis equal to the destroyin Java 8.
So you should try to kill the process by pid if process is still alive after you called the destroymethod. You can easily get the pid if you are on java 9+,just call Process.pid()method.

我只想说,在 Java 8 中的destroyForcibly等于destroy
因此,如果在调用该destroy方法后进程仍然存在,则应尝试通过 pid 终止进程。如果您使用的是 Java 9+,则可以轻松获取 pid,只需调用Process.pid()方法即可。