Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task manager with Java

Tags:

java

windows

Hi so I'm a student in high school and have a project to do with CS. I'm trying to write a program that shuts down an external application after a certain amount of time has passed. That time part was pretty easy to do but now the problem is the actual shutting down of the problem. I looked around and couldn't find anything that worked for me and need some help with this.

Here's what I have so far:

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

UPDATED CODE:

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

Still needs some working on when it comes to Mac OS. But the windows part works great. Added some video games that i had on my computer to shut down.

like image 245
Kevin Avatar asked Sep 06 '25 03:09

Kevin


1 Answers

Use System#getProperty(String) to determine which OS is running the program:

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
}

Then kill the task using the appropriate method:

For Windows

You could use the method Runtime#exec(String) to execute the taskkill command via cmd.exe:

Runtime.getRuntime().exec("cmd /c taskkill /f /IM process.exe");

Replace process.exe with the filename of the process you want to kill. You may have to run the program as administrator, since taskkill requires elevation to kill some processes.

For Mac

For mac, you will use the same method, just passing different arguments to it. Consider the following:

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

or

Runtime.getRuntime().exec("/bin/bash -c sudo killall process PID");
like image 99
Cardinal System Avatar answered Sep 07 '25 20:09

Cardinal System