Java任务管理器

时间:2017-03-30 03:30:55

标签: java windows

嗨,所以我是高中生,并且有一个与CS有关的项目。我正在尝试编写一个程序,在经过一段时间后关闭外部应用程序。那个时间部分很容易做,但现在问题是实际关闭问题。我环顾四周,找不到任何对我有用的东西,需要一些帮助。

这是我到目前为止所拥有的:

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上还需要一些工作。但是窗户部分效果很好。添加了一些我在计算机上关闭的视频游戏。

1 个答案:

答案 0 :(得分:5)

使用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");
相关问题