如何在 java 中终止任务管理器?

How to kill Task Manager in java?

在我的程序中,如果它是 运行,我想终止任务管理器。我试过这个:

 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);
}

但任务管理器仍然是 运行。我能做什么?

您是否尝试过更改流程的大小写?
例如:

if(isProcessRunning("taskmgr.exe")){
    killProcess("taskmgr.exe");
}

好的,要杀死任务管理器,您需要管理员权限。对于此下载 this.

现在复制任何一个文件 Elevate.exe 或 Elevate64.exe 到您的 java 项目源路径中。

然后

Runtime.getRuntime().exec("src//Elevate64.exe TASKKILL /F /IM Taskmgr.exe"); 

现在它会提示是或否。单击是....然后 BOOM

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());
}

没有必要明确检查任务是否为 运行。如果找不到,则不会执行该命令。尝试杀死它,如果出现异常,则在 catch 块中解决它。