如何在 Java 9 中使用 Process API 终止/销毁进程?

2023年 9月 11日 59.6k 0

如何在 Java 9 中使用 Process API 终止/销毁进程?

在 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 Process process = new ProcessBuilder(javaCmd, "-cp", classpath, DestroyProcessTest.class.getName()).start();
ProcessHandle processHandle = process.toHandle();
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.destroy();
}
private static void printInfo(ProcessHandle processHandle) {
System.out.println("---------");
System.out.println("Id: " + processHandle.pid());
System.out.println("isAlive(): " + processHandle.isAlive());
System.out.println("isSupportsNormalTermination(): " + processHandle.supportsNormalTermination());

ProcessHandle.Info processInfo = processHandle.info();
System.out.println("Info: " + processInfo.toString());
System.out.println("Info arguments().isPresent(): " + processInfo.arguments().isPresent());
System.out.println("Info command().isPresent(): " + processInfo.command().isPresent());
System.out.println("Info totalCpuDuration().isPresent(): " + processInfo.totalCpuDuration().isPresent());
System.out.println("Info user().isPresent(): " + processInfo.user().isPresent());
}
}

登录后复制

输出

---------------------------
Destroy Process:
---------
Id: 4384
isAlive(): true
isSupportsNormalTermination(): false
Info: [user: Optional[TutorialspointUser], cmd: C:Program FilesJavajdk-9.0.4binjava.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[TutorialspointUser], 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

登录后复制

以上就是如何在 Java 9 中使用 Process API 终止/销毁进程?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论