如何在 Windows 中杀死 Java 进程,而不杀死 javaw.exe?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3696077/
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
How to Kill Java Process in Windows, WITHOUT killing javaw.exe?
提问by Android Eve
I have an external Windows .exe that is actually Java application: Running the .exe starts javaw.exe, which in turn runs that Java application.
我有一个外部的 Windows .exe,它实际上是 Java 应用程序:运行 .exe 会启动 javaw.exe,它依次运行该 Java 应用程序。
I didn't write that application and have no access to it through an API. I need to be able to kill it, however. So right now I just kill the Windows process javaw.exe, which is fine for a test machine running only that Java application but if I need finer granularity, I cannot currently do so.
我没有编写该应用程序,也无法通过 API 访问它。但是,我需要能够杀死它。所以现在我只是终止了 Windows 进程 javaw.exe,这对于仅运行该 Java 应用程序的测试机器来说很好,但是如果我需要更精细的粒度,我目前无法这样做。
My searches yielded suggestions such as Sysinternal's Process Exploreror the jps command in the JDK, but in the target systems for which I intend to provide the script, neither JDK nor Sysinternal's Process Explorer can be running.
我的搜索产生了一些建议,例如Sysinternal 的 Process Explorer或 JDK 中的 jps 命令,但在我打算为其提供脚本的目标系统中,JDK 和 Sysinternal 的 Process Explorer 都无法运行。
Is there any other way that doesn't require an external tool? Does javaw.exe have a switch or command line option that lists Java processes? Is there a JRE version of jps?
有没有其他不需要外部工具的方法?javaw.exe 是否有列出 Java 进程的开关或命令行选项?jps有JRE版本吗?
Thanks.
谢谢。
采纳答案by mikera
I'd still suggest just killing the javaw.exe.
我仍然建议杀死 javaw.exe。
I can't see the downside, since it is the process you want to kill after all.
我看不出缺点,因为它毕竟是你想要杀死的进程。
Remember that if you run multiple applications on the machine, they should each have a separate JVM instance. So you can still kill the specific application if you need to.
请记住,如果您在机器上运行多个应用程序,则每个应用程序都应该有一个单独的 JVM 实例。因此,如果需要,您仍然可以终止特定的应用程序。
回答by basszero
The JDK (and possibly the JRE) ship with a utility called jps which can list all Java processes but also tell you the Main-Class currently running in that JVM. If JMX/JConsole is not an option, simply parsing the output of "jps -ml" and killing the appropriate process may work.
JDK(也可能是 JRE)附带了一个名为 jps 的实用程序,它可以列出所有 Java 进程,还可以告诉您当前在该 JVM 中运行的 Main-Class。如果 JMX/JConsole 不是一个选项,简单地解析“jps -ml”的输出并终止相应的进程可能会起作用。
回答by Jim Garrison
If you want to kill an entire JVM, just kill the javaw.exe process. Within a JVM there can be multiple Java threadsbut there's no way to poke into a JVM and terminate a thread unless the developer of the application provided a method to do so.
如果您想杀死整个 JVM,只需杀死 javaw.exe 进程。在 JVM 中可以有多个 Java线程,但是除非应用程序的开发人员提供了一种方法,否则无法进入 JVM 并终止线程。
回答by Jim Rush
Based on your comment, multiple javaw.exe programs are running and you need to know which one to kill.
根据您的评论,多个 javaw.exe 程序正在运行,您需要知道要杀死哪个程序。
You might want to try connecting to each of the processes with JConsole and inspect the JVM. There may be enough clues to determine which one to kill. Once you've identified the profile of your application, you should be able to script the logic to make it easier in the future (use JMX to get most of the information provided by JConsole).
您可能想尝试使用 JConsole 连接到每个进程并检查 JVM。可能有足够的线索来确定要杀死哪一个。一旦确定了应用程序的配置文件,您就应该能够编写逻辑脚本以使其在未来更容易(使用 JMX 获取 JConsole 提供的大部分信息)。
回答by Fredrick Pennachi
If the executable launches javaw then exits without providing any further information it seems like you need to use your scripting language to take a snapshot of running processes on the machine before launching the executable and after the executable has finished. Then you'll be able to deduce which is the new javaw process. What scripting language are you using?
如果可执行文件启动 javaw 然后退出而不提供任何进一步的信息,似乎您需要使用脚本语言在启动可执行文件之前和可执行文件完成后拍摄机器上正在运行的进程的快照。然后你就可以推断出哪个是新的 javaw 进程。你使用什么脚本语言?
回答by Ward
Just another approach: if you have the jdk, there is a program called jvisualVM in the bin folder. It has nice info about each running JVM context. One of the things you can see is the PID of the VM, which I use to kill the process in Windows using task manager (on windows PID is not shown by default, but you can easily enable the column by going into view -> show columns )
只是另一种方法:如果您有 jdk,则 bin 文件夹中有一个名为 jvisualVM 的程序。它有关于每个正在运行的 JVM 上下文的很好的信息。您可以看到的一件事是 VM 的 PID,我用它来使用任务管理器在 Windows 中终止进程(在 Windows 上,PID 默认不显示,但您可以通过进入视图轻松启用该列 -> 显示列 )