gpt4 book ai didi

java - 如何从 Java 中查找并杀死正在运行的 Win-Processes?

转载 作者:IT老高 更新时间:2023-10-28 20:25:10 27 4
gpt4 key购买 nike

我需要一种 Java 方法来查找正在运行的 Win 进程,我知道从中可以找到可执行文件的名称。我想看看它现在是否正在运行,如果找到它,我需要一种方法来终止该进程。

最佳答案

private static final String TASKLIST = "tasklist";
private static final String KILL = "taskkill /F /IM ";

public static boolean isProcessRunning(String serviceName) throws Exception {

Process p = Runtime.getRuntime().exec(TASKLIST);
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {

System.out.println(line);
if (line.contains(serviceName)) {
return true;
}
}

return false;

}

public static void killProcess(String serviceName) throws Exception {

Runtime.getRuntime().exec(KILL + serviceName);

}

示例:

public static void main(String args[]) throws Exception {
String processName = "WINWORD.EXE";

//System.out.print(isProcessRunning(processName));

if (isProcessRunning(processName)) {

killProcess(processName);
}
}

关于java - 如何从 Java 中查找并杀死正在运行的 Win-Processes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/81902/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com