3

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.

Kevin
  • 33
  • 1
  • 5
  • I assume you mean shutdown an arbitrary process, not your program or a process your program created – MadProgrammer Mar 30 '17 at 03:32
  • Yes, not the program itself just the process like chrome or notepad. – Kevin Mar 30 '17 at 03:34
  • You won't be able to do this (easily) from Java. You could explore utilising command line commands through ProcessBuilder or using JNI/JNA to connect to the native os libraries – MadProgrammer Mar 30 '17 at 03:41
  • Here's a question very similar to yours on how to destroy processes. http://stackoverflow.com/questions/6356340/killing-a-process-using-java – Raheel138 Mar 30 '17 at 03:43
  • @Raheel138 so what i understood from your post's top comment is that if you start chrome inside of the java program itself, you can then close it to? but if its running from outside the program then you will have a harder time? – Kevin Mar 30 '17 at 03:54
  • That's what I understood from it. If you're running it from outside the program, you either have to use a native interface like JNI or JNA like MadProgrammer mentioned or you have to make it platform dependent on Unix or Linux. If you run it from within the program, there is only the issue of ensuring there are no processes left running and the fact that all bugs have to be accounted for. – Raheel138 Mar 30 '17 at 03:59
  • Ok, I updated my answer with a solution for macs =) – Cardinal System Mar 31 '17 at 02:01
  • so i tried the solution for macs in school and it compiled but it didnt run. is it possible that it wont run because of the command that it is trying to execute and the administrator settings dont allow it to? – Kevin Mar 31 '17 at 03:52

1 Answers1

7

Use System.getProperty("os.name"); 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 based on the OS:

---- For Windows ---

You could use the method Runtime.getRuntime().exec(); to execute a cmd command in order to kill the process.

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

Replacing process.exe with the filename of the process you want to kill. You might have to run the program as administrator, because 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");
Cardinal System
  • 2,749
  • 3
  • 21
  • 42