如果我具有特定的PID,如何在Windows上从Java代码中终止特定的进程。
除了执行像Runtime.getRuntime().exec("taskkill /F /PID 827");
这样的特定Windows
命令外,我不知道其他解决方案Runtime.getRuntime().exec("taskkill /F /PID 827");
With Java 9
, we can use ProcessHandle :
ProcessHandle.of(11395).ifPresent(ProcessHandle::destroy);
where 11395
is the pid
of the process you're interested in killing.
This:
First creates an Optional<ProcessHandle>
from the given pid
And if this ProcessHandle
is present, kills the process using destroy
.
No import necessary as ProcessHandle
is part of java.lang
.
To force-kill the process, one might prefer ProcessHandle::destroyForcibly
to ProcessHandle::destroy
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.