在我的程序中,如果任務管理器正在運行,我想殺死它。我試過這個: private static final String TASKLIST = "tasklist";private static final String KILL = "taskkill /F /IM ";if(isProcessRunning("Taskmgr.exe")){ // TODO code application logic here killProcess("Taskmgr.exe");}這是我的isProcessRunning()方法: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)) { System.err.println(line); return true; } } return false;}和killprocess()方法:public static void killProcess(String serviceName) throws Exception { Runtime.getRuntime().exec(KILL + serviceName);}但任務管理器仍在運行。我能做什么?
3 回答

有只小跳蛙
TA貢獻1824條經(jīng)驗 獲得超8個贊
try{
Process p = Runtime.getRuntime()
p.exec("taskkill /F /IM taskmgr.exe /T")
catch(*Enter applicable exception type here* e){
System.err.println("Caught Exception: " + e.getMessage());
}
沒有必要明確檢查任務是否正在運行。如果未找到,則不會執(zhí)行該命令。嘗試殺死它,如果出現(xiàn)異常,則在 catch 塊中考慮它。

呼喚遠方
TA貢獻1856條經(jīng)驗 獲得超11個贊
您是否嘗試過更改流程的案例?
例如:
if(isProcessRunning("taskmgr.exe")){
killProcess("taskmgr.exe");
}
添加回答
舉報
0/150
提交
取消