使用 Java 代码终止/停止应用程序

发布于 2024-12-19 11:42:00 字数 95 浏览 4 评论 0原文

是否可以使用 java 代码停止 Windows 中的应用程序? 我想创建一个程序,就像停止我的电脑上的应用程序的快捷方式。是否可以使用 ProcessBuilder ?谢谢

Is it possible to stop an application in windows using java code ?
I'd like to create a program which is like a shortcut to stop an application on my pc. Is it possible using ProcessBuilder ? Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟酒忠诚 2024-12-26 11:42:00

纯 Java,没有本机访问?不可以

。ProcessBuilder 允许您控制从 Java 应用程序中启动的进程,但它不能控制由其他进程启动的进程。

根据 user988052 的评论,您可以使用 Sysinternals Suite< 中的 pskill /a> 通过 Runtime.exec 来完成此操作。 pskill 使用 TerminateProcess 函数调用。您还可以使用 JNI(或其他一些本机调用程序,如 JNA、NLink 等)调用此函数,但您需要通过 OpenProcess 函数调用。

Plain Java, without native access? No.

ProcessBuilder lets you control processes that you started from within the Java application, but it doesn't give you control to processes that were started by other processes.

As per user988052's comment, you can use pskill from the Sysinternals Suite to accomplish this through Runtime.exec. pskill uses the TerminateProcess function call. You can also call this function with JNI (or some other native caller, like JNA, NLink, etc.), but you'll need to obtain a handle to the process via the OpenProcess function call.

━╋う一瞬間旳綻放 2024-12-26 11:42:00

是否可以使用 java 代码停止 Windows 中的应用程序? ...可以使用 ProcessBuilder 吗?

仅当您从尝试填充该应用程序的同一个 Java 应用程序启动该应用程序时。

除此之外,您需要诉诸于运行特定于 Windows 的命令来执行查杀...或基于 JNI 的命令来进行本机 Windows 库调用。

其他复杂情况是,由于权限问题,您可能无法杀死某些进程,并且某些进程可能拒绝被杀死。

Is it possible to stop an application in windows using java code ? ... Is it possible using ProcessBuilder ?

Only if you launched the application from the same Java application that you are trying to fill it from.

Other than that, you need to resort to running a Windows-specific command to do the killing ... or something based on JNI to make native Windows library calls.

The other complications are that you may not be able to kill certain processes due to permissions issues, and some process may refuse to be killed.

思慕 2024-12-26 11:42:00

我相信 JRE 不提供系统进程的 API。 ProcessBuilder 可以创建系统进程,但它无法为您提供在 JVM 外部创建的进程的Process对象。我相信这是 Java 人员的一个哲学决定,尽可能避免某些特定于操作系统的任务。您可能认为每个操作系统都有 pid,或者它们都有某种终止消息,但 Java 可以在很多操作系统上运行,其中一些操作系统非常奇怪。

您可以编写本机代码来执行此操作并用 JNI 包装它,但这会很浪费。正如988052先生所说,我建议您通过Runtime.execProcessBuilder执行系统特定的命令。您需要决定要支持哪些操作系统,并确保为每个操作系统提供正确的命令(和操作系统检测代码)。

I believe the JRE does not provide an API for system processes. ProcessBuilder can create system processes, but it cannot provide you with a Process object for a process created outside the JVM. I believe that is a philosophical decision on the part of the Java folks, to avoid certain OS-specific tasks where possible. You might think that every OS has pids, or that they all have some kind of kill message, but Java runs on lots of OSes, some of them quite strange.

You could make native code to do it and wrap that with JNI, but that would be wasteful. As Mr 988052 says, I suggest you execute a system-specific command through Runtime.exec or ProcessBuilder. You would need to decide which OSes you want to support, and be sure to get the commands (and the OS-detection code) right for each one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文