如何在 Windows 中杀死 Java 进程而不杀死 javaw.exe?
我有一个外部 Windows .exe,它实际上是 Java 应用程序:运行 .exe 会启动 javaw.exe,后者又运行该 Java 应用程序。
我没有编写该应用程序,也无法通过 API 访问它。然而,我需要能够杀死它。所以现在我只是终止 Windows 进程 javaw.exe,这对于仅运行该 Java 应用程序的测试机来说很好,但如果我需要更精细的粒度,我目前无法这样做。
我的搜索产生了一些建议,例如 Sysinternal 的 Process Explorer 或JDK,但在我打算为其提供脚本的目标系统中,JDK 和 Sysinternal 的 Process Explorer 都无法运行。
有没有其他不需要外部工具的方法? javaw.exe 是否有列出 Java 进程的开关或命令行选项? jps 有 JRE 版本吗?
谢谢。
I have an external Windows .exe that is actually Java application: Running the .exe starts javaw.exe, which in turn runs that Java application.
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.
My searches yielded suggestions such as Sysinternal's Process Explorer or 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.
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?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我仍然建议杀死 javaw.exe。
我看不出有什么缺点,因为这毕竟是您想要终止的进程。
请记住,如果您在计算机上运行多个应用程序,则它们每个都应该有一个单独的 JVM 实例。因此,如果需要,您仍然可以终止特定的应用程序。
I'd still suggest just killing the 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.
JDK(可能还有 JRE)附带了一个名为 jps 的实用程序,它可以列出所有 Java 进程,还可以告诉您当前在该 JVM 中运行的主类。如果 JMX/JConsole 不是一个选项,只需解析“jps -ml”的输出并终止相应的进程即可。
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.
如果你想杀死整个 JVM,只需杀死 javaw.exe 进程即可。在 JVM 中可以有多个 Java 线程,但是除非应用程序的开发人员提供了一种方法来执行此操作,否则无法深入 JVM 并终止线程。
If you want to kill an entire JVM, just kill the javaw.exe process. Within a JVM there can be multiple Java threads but 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.
根据您的评论,多个 javaw.exe 程序正在运行,您需要知道要杀死哪一个。
您可能想要尝试使用 JConsole 连接到每个进程并检查 JVM。可能有足够的线索来确定要杀死哪一个。一旦确定了应用程序的配置文件,您应该能够编写逻辑脚本,以便将来更容易使用(使用 JMX 获取 JConsole 提供的大部分信息)。
Based on your comment, multiple javaw.exe programs are running and you need to know which one to kill.
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).
如果可执行文件启动 javaw 然后退出而不提供任何进一步的信息,那么您似乎需要在启动可执行文件之前和可执行文件完成之后使用脚本语言来拍摄计算机上正在运行的进程的快照。然后您将能够推断出哪个是新的 javaw 进程。您使用什么脚本语言?
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?
另一种方法是:如果你有jdk,则bin文件夹中有一个名为jvisualVM的程序。它包含有关每个正在运行的 JVM 上下文的详细信息。您可以看到的其中一件事是虚拟机的 PID,我用它来使用任务管理器终止 Windows 中的进程(在 Windows 上,默认情况下不显示 PID,但您可以通过进入视图 -> 轻松启用该列)。显示列)
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 )