如何从另一个程序中以编程方式关闭在 Windows 中运行的特定 java 应用程序? [英] How can I programatically close a SPECIFIC java application running in windows from within another program?

查看:30
本文介绍了如何从另一个程序中以编程方式关闭在 Windows 中运行的特定 java 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从另一个程序中(强制)关闭一个特定的 Java 应用程序.对于任何本机程序杀死它的进程或将 vm_close 发送到它的主窗口通常都可以工作,但对于在 Windows 上运行的 Java 应用程序则不然.请注意,Java 应用程序在正常状态下仅显示托盘图标,并没有打开窗口.

I need to (force)close a specific java application from within another program. For any native program killing it's process or sending vm_close to it's main window usually works but not so much with java applications running on windows. Do note that the java application is only showing a tray icon and has no open window during it's normal state.

推荐答案

如果您知道 Java 程序的 PID,请使用:

If you know your Java program's PID, use this:

Runtime.getRuntime().exec("taskkill /F /PID bla");

其中 bla 是您程序的 PID.

where bla is your program's PID.

那个 /F 参数会强行杀死一个应用程序,我看不出 Java 程序与其他程序有什么不同并且能够幸存下来的原因.

That /F parameter will forcefully kill an application and I don't see a reason why a Java program would be any different from other programs and be able to survive that.

请注意,此代码显然是特定于 Windows 的.如果您希望它具有可移植性,您应该首先使用

Note that this code is obviously Windows specific. If you want it to be portable, you should first find out what OS is hosting your application by using

System.getProperty("os.name")

之后,如果您使用的是 Windows,请使用 taskkill/F,如果您使用的是 *nix 系统,请使用 kill -9,或者如果您使用其他系统,请使用其他东西'在一个更奇特的平台上.

and afterwards, use taskkill /F if you're on Windows, or kill -9 if you're on a *nix system, or something else if you're on a more exotic platform.

更新:要对所有正在运行的进程有一个很好的概述,我建议使用 流程浏览器.如果您需要命令行进程列表,请使用 PsList.

Update: to have a nice overview of all running processes, I'd recommend Process explorer. If you want a command line process lister, use PsList.

如果你想杀死一个特定的 Java 程序,它可能会出现问题,因为默认情况下每个 Java 进程都被命名为 java.exejavaw.exe.

If you want to kill a specific Java program, it can be problematic since every Java process by default is named java.exe or javaw.exe.

使用 PsList,您可以以编程方式扫描所有以 java 开头的进程,然后结合 jps,您将获得有关您真正想要杀死的内容的更多信息.

Using PsList, you can programatically scan all processes which start with java and then, combined with jps, you'll have more information about what you really want to kill.

这篇关于如何从另一个程序中以编程方式关闭在 Windows 中运行的特定 java 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆