package socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class KillProcessUtils {
/**
* 手动杀死bartender进程
* @throws IOException
*/
public static void killProcess() throws IOException {
String processName = "bartend"; // 将此处替换为目标进程的名称或关键字
ProcessBuilder pb = new ProcessBuilder("taskkill", "/F", "/IM", processName + ".exe");
Process process = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuilder output = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
int exitCode = -1;
try {
exitCode = process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (exitCode == 0) {
System.out.println("成功杀死进程!");
} else {
System.err.println("无法杀死进程:" + output);
}
process.destroy();
}
}
}
参考文献:百度AI