我现在正在做这件事;
public class ProcessKill {
private static final String TASKLIST = "tasklist";
private static final String KILL = "taskkill /IM ";
public static boolean isProcessRunging(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{
ProcessKill pkill = new ProcessKill();
String processName = "wmplayer.exe";
if (pkill.isProcessRunging(processName)){
pkill.killProcess(processName);
}
}
唯一的问题是,如果改名怎么办?我不想继续更新代码。有没有办法获取 .exe 文件的十六进制值并将其读入 Java 以检测它?
谢谢