如何在 Java 9 中使用 Process API 终止/销毁进程?
Sep 11, 2023 pm 03:37 PM在 Java 9 中,Process API 支持一种简单的方法来获取有关进程的大量信息。 ProcessHandle接口可以识别并提供对本机进程的控制以及检查进程活动性和销毁进程的方法,而ProcessHandle.Info接口可以提供进程的信息快照。我们需要使用ProcessHandle接口的destroy()方法来销毁进程。
在下面的示例中,我们需要使用ProcessHandle接口终止进程。
示例
import java.io.File; import java.io.IOException; import java.util.Objects; public class DestroyProcessTest { public static void main(String[] args) throws InterruptedException { System.out.println("---------------------------"); System.out.println("Destroy Process:"); final String javaCmd = getJavaCmdFromParent(); final String classpath = getClassPathFromParent(); try { final <strong>Process </strong>process = new <strong>ProcessBuilder</strong>(javaCmd, "-cp", classpath, <strong>DestroyProcessTest.class.getName()</strong>).start(); ProcessHandle processHandle = process.<strong>toHandle()</strong>; printInfo(processHandle); destroyProcess(processHandle); Thread.sleep(1000); System.out.println("---------------------------"); System.out.println("After destroying the process:"); printInfo(processHandle); } catch(IOException e) { e.printStackTrace(); } } private static String getClassPathFromParent() { return System.getProperty("java.class.path", "./*"); } private static String getJavaCmdFromParent() { return Objects.isNull(System.getProperty("java.home")) ? "java" : String.format("%s%sbin%sjava", System.getProperty("java.home"), File.separator, File.separator); } private static void destroyProcess(ProcessHandle processHandle) throws IllegalStateException { System.out.println("Ready to destroy Process with id: " + processHandle.pid()); processHandle.<strong>destroy()</strong>; } private static void printInfo(ProcessHandle processHandle) { System.out.println("---------"); System.out.println("Id: " +<strong> </strong>processHandle<strong>.pid()</strong>); System.out.println("isAlive(): " +<strong> </strong>processHandle<strong>.isAlive()</strong>); System.out.println("isSupportsNormalTermination(): " + processHandle.<strong>supportsNormalTermination()</strong>); <strong>ProcessHandle.Info</strong> processInfo = <strong>processHandle.info()</strong>; System.out.println("Info: " + processInfo<strong>.toString()</strong>); System.out.println("Info arguments().isPresent(): " + processInfo<strong>.</strong><strong>arguments()</strong><strong>.isPresent()</strong>); System.out.println("Info command().isPresent(): " + <strong>processInfo.command().isPresent()</strong>); System.out.println("Info totalCpuDuration().isPresent(): " + processInfo.<strong>totalCpuDuration().isPresent()</strong>); System.out.println("Info user().isPresent(): " + processInfo.<strong>user().isPresent()</strong>); } }
登录后复制
输出
<strong>--------------------------- Destroy Process: --------- Id: 4384 isAlive(): true isSupportsNormalTermination(): false Info: [user: Optional[Tutorialspoint\User], cmd: C:\Program Files\Java\jdk-9.0.4\bin\java.exe, startTime: Optional[2020-03-06T10:58:53.210Z], totalTime: Optional[PT0.046875S]] Info arguments().isPresent(): false Info command().isPresent(): true Info totalCpuDuration().isPresent(): true Info user().isPresent(): true Ready to destroy Process with id: 4384 --------------------------- After destroying the process: --------- Id: 4384 isAlive(): false isSupportsNormalTermination(): false Info: [user: Optional[Tutorialspoint\User], startTime: Optional[2020-03-06T10:58:53.210Z], totalTime: Optional[PT0.109375S]] Info arguments().isPresent(): false Info command().isPresent(): false Info totalCpuDuration().isPresent(): true Info user().isPresent(): true</strong>
登录后复制
以上是如何在 Java 9 中使用 Process API 终止/销毁进程?的详细内容。更多信息请关注PHP中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章标签

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

2025年的前4个JavaScript框架:React,Angular,Vue,Svelte

如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?

如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?

如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?

Spring Boot Snakeyaml 2.0 CVE-2022-1471问题已修复
