Java任务管理器

3

您好,我是一名高中学生,现在正在进行一项与计算机科学有关的项目。我尝试编写一个程序,在一定时间后关闭外部应用程序。这个时间部分相当容易实现,但现在问题是如何真正关闭应用程序。我搜索了一下,发现没有找到适合我的解决方法,需要一些帮助。

以下是我目前的进展:

import java.util.concurrent.TimeUnit;
import javax.swing.JOptionPane;
public class TaskKiller {
    public static void main(String[] args) {
        JOptionPane pane = new JOptionPane();
        String userInput1 = JOptionPane.showInputDialog("Hours: "); 
        String userInput2 = JOptionPane.showInputDialog("Minutes: ");
        String userInput3 = JOptionPane.showInputDialog("Seconds: ");
        long inputHours = Long.parseLong(userInput1);
        long inputMinutes = Long.parseLong(userInput2);
        long inputSeconds = Long.parseLong(userInput3);
        long hours = inputHours * 3600000;
        long minutes = inputMinutes * 60000;
        long seconds = inputSeconds * 1000;
        long time = (hours + minutes) + seconds;
        try {
            TimeUnit.MILLISECONDS.sleep(time);
            System.exit(0);//was just an idea
            System.out.println("You sucessfully closed the program");
        }
        catch (InterruptedException ex) {
        }
    } 
}

更新的代码:

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import java.lang.*;
public class TaskKiller {
@SuppressWarnings("IndexOfReplaceableByContains")
public static void main(String[] args) {
    JOptionPane pane = new JOptionPane();
    String userInput1 = JOptionPane.showInputDialog("Hours: "); 
    String userInput2 = JOptionPane.showInputDialog("Minutes: ");
    String userInput3 = JOptionPane.showInputDialog("Seconds: ");
    long inputHours = Long.parseLong(userInput1);
    long inputMinutes = Long.parseLong(userInput2);
    long inputSeconds = Long.parseLong(userInput3);
    long hours = inputHours * 3600000;
    long minutes = inputMinutes * 60000;
    long seconds = inputSeconds * 1000;
    long time = (hours + minutes) + seconds;
    String OS = System.getProperty("os.name").toLowerCase();
 int run = 1;   



while (run > 0){
 JOptionPane.showMessageDialog(null,"Program names: chrome, firefox, safari, gta, csgo", "Info", JOptionPane.INFORMATION_MESSAGE);
    String prog = JOptionPane.showInputDialog("Program: ");
    String gta="gta";
    String chrome="chrome";
    String firefox="firefox";     
    String safari="safari";   
    String csgo="csgo";       
    int gt=prog.compareTo(gta);
    int ch=prog.compareTo(chrome);
    int fi=prog.compareTo(firefox);
    int sa=prog.compareTo(safari);
    int cs=prog.compareTo(csgo);

if (gt == 0) {
    if(OS.indexOf("win") >= 0){
    try {
        TimeUnit.MILLISECONDS.sleep(time);
    Runtime.getRuntime().exec("cmd /c Taskkill /IM GTA5.exe /F");
        TimeUnit.MILLISECONDS.sleep(5000);
        Runtime.getRuntime().exec("cmd /c Taskkill /IM GTAVLauncher.exe /F");
        System.out.println("Win");
        run=run-1;
    }
    catch (InterruptedException ex) {
    } 
    catch (IOException ex) {
        Logger.getLogger(TaskKiller.class.getName()).log(Level.SEVERE, null, ex);
    }
    }else if(OS.indexOf("mac") >= 0){
        try {
            Runtime.getRuntime().exec("/bin/bash -c  sudo killall process PID");
            System.out.println("Mac");
            run=run-1;
        } catch (IOException ex) {
            Logger.getLogger(TaskKiller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
else if (ch==0) {
if(OS.indexOf("win") >= 0){
    try {
        TimeUnit.MILLISECONDS.sleep(time);
    Runtime.getRuntime().exec("cmd /c Taskkill /IM chrome.exe /F");
        System.out.println("Win");
        run=run-1;
    }
    catch (InterruptedException ex) {
    } 
    catch (IOException ex) {
        Logger.getLogger(TaskKiller.class.getName()).log(Level.SEVERE, null, ex);
    }
    }else if(OS.indexOf("mac") >= 0){
        try {
            Runtime.getRuntime().exec("/bin/bash -c  sudo killall process PID");
            System.out.println("Mac");
            run=run-1;
        } catch (IOException ex) {
            Logger.getLogger(TaskKiller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
else if (fi==0) {
    if(OS.indexOf("win") >= 0){
    try {
        TimeUnit.MILLISECONDS.sleep(time);
    Runtime.getRuntime().exec("cmd /c Taskkill /IM firefox.exe /F");
        System.out.println("Win");
        run=run-1;
    }
    catch (InterruptedException ex) {
    } 
    catch (IOException ex) {
        Logger.getLogger(TaskKiller.class.getName()).log(Level.SEVERE, null, ex);
    }
    }else if(OS.indexOf("mac") >= 0){
        try {
            Runtime.getRuntime().exec("/bin/bash -c  sudo killall process PID");
            System.out.println("Mac");
            run=run-1;
        } catch (IOException ex) {
            Logger.getLogger(TaskKiller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
else if (sa==0) {
    if(OS.indexOf("win") >= 0){
    try {
        TimeUnit.MILLISECONDS.sleep(time);
    Runtime.getRuntime().exec("cmd /c Taskkill /IM safari.exe /F");
        System.out.println("Win");
        run=run-1;
    }
    catch (InterruptedException ex) {
    } 
    catch (IOException ex) {
        Logger.getLogger(TaskKiller.class.getName()).log(Level.SEVERE, null, ex);
    }
    }else if(OS.indexOf("mac") >= 0){
        try {
            Runtime.getRuntime().exec("/bin/bash -c  sudo killall process PID");
            System.out.println("Mac");
            run=run-1;
        } catch (IOException ex) {
            Logger.getLogger(TaskKiller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
else if (cs==0) {
    if(OS.indexOf("win") >= 0){
    try {
        TimeUnit.MILLISECONDS.sleep(time);
    Runtime.getRuntime().exec("cmd /c Taskkill /IM csgo.exe /F");
        System.out.println("Win");
        run=run-1;
    }
    catch (InterruptedException ex) {
    } 
    catch (IOException ex) {
        Logger.getLogger(TaskKiller.class.getName()).log(Level.SEVERE, null, ex);
    }
    }else if(OS.indexOf("mac") >= 0){
        try {
            Runtime.getRuntime().exec("/bin/bash -c  sudo killall process PID");
            System.out.println("Mac");
            run=run-1;
        } catch (IOException ex) {
            Logger.getLogger(TaskKiller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
else {
    JOptionPane.showMessageDialog(null, "Invalid name. Try Again or quit", "Error", JOptionPane.ERROR_MESSAGE);
    run=run+1;
}
}
}
}

在Mac OS方面仍需要一些改进。但是Windows部分运行良好。将一些我在电脑上有的视频游戏添加到关闭列表中。


我假设你想关闭一个任意的进程,而不是你的程序或者你的程序创建的一个进程。 - MadProgrammer
你无法轻松地从Java中完成这个任务。你可以尝试通过ProcessBuilder使用命令行命令,或者使用JNI/JNA连接到本地操作系统库。 - MadProgrammer
这里有一个非常类似于你的问题的问题,关于如何销毁进程。https://dev59.com/eGw15IYBdhLWcg3w3ffN - Raheel138
这是我从中理解到的。如果你是从程序外部运行它,你要么必须使用像JNI或JNA这样的本地接口,就像MadProgrammer提到的那样,要么你必须使其在Unix或Linux上依赖于平台。如果你从程序内部运行它,只需要确保没有剩余进程,并且所有的错误都得到了解决。 - Raheel138
所以我尝试了在学校Mac上的解决方案,它编译通过了,但是没有运行。这可能是因为它正在尝试执行的命令被管理员设置禁止了,导致无法运行吗? - Kevin
显示剩余3条评论
1个回答

7
使用System.getProperty("os.name");来确定运行程序的操作系统:
String os = System.getProperty("os.name").toLowerCase();
if(os.indexOf("win") >= 0) {
//run windows process kill method
} else if(os.indexOf("mac") >= 0) {
//run mac process kill method
}

然后根据操作系统来终止任务:

---- 对于Windows ---

您可以使用方法Runtime.getRuntime().exec();来执行命令cmd以终止进程。

Runtime.getRuntime().exec("cmd /c Taskkill /IM process.exe /F");

process.exe替换为您想要终止的进程的文件名。您可能需要以管理员身份运行程序,因为taskkill需要提升权限才能杀死某些进程。

--- 对于Mac ---

对于Mac,您将使用相同的方法,只需向其传递不同的参数即可。考虑以下内容:

Runtime.getRuntime().exec("/bin/bash -c  sudo kill process PID");

或者

Runtime.getRuntime().exec("/bin/bash -c  sudo killall process PID");

没问题!很高兴能帮忙 =D - Cardinal System
我的声望低于15,所以它不让我做。 - Kevin
1
真的很快的问题。这在Windows上可以工作,但是在Mac上命令是一样的吗? - Kevin
1
我让它正常工作了。只需要加入几行其他的代码。感谢所有的帮助。 - Kevin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接