Program to shutdown computer after a certain time I'm trying to make a program that will shutdown my computer after a certain time that I will input via GUI. I'm wondering if I did it the correct way.

Also, will this slow down my computer while it waits to shut off? Do you see anything wrong with the code?

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class PCShutdown {
    private int minutes;
    private int hours;

    private void setHours(int hoursArg) {
        this.hours = hoursArg;
    }

    private void setMinutes(int minutesArg) {
        this.minutes = minutesArg;
    }

    private void shutDown() {
        Thread shutDownThread = new Thread(new ShutDownThread());
        shutDownThread.start();
    }

    private class ShutDownThread implements Runnable {

        @Override
        public void run() {
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    Runtime runtime = Runtime.getRuntime();
                    Process process = null;
                    try {
                        process = runtime.exec("shutdown -s -t 30");
                        System.exit(0);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally { process.destroy();}
                }
            }, (hours * 60 * 60 * 1000) + (minutes * 60 * 1000));
        }
    }
}


Also, I still have to figure out how to kill all open programs except for this one before it shuts down. I'm going to work on that now.

Also, this might be a very dumb question, but will any of these operations harm my computer in any way? Will it be better if I just learn to do this via powershell?
Your questions at the end are the most important points here, so I answer those first:


  but will any of these operations harm my computer in any way?


No, and how could it anyway?


  Will it be better if I just learn to do this via powershell?


Yes! Writing a Java program for this is overkill!

In terms of a Java implementation, I think you're really overcomplicating it:


Why use a thread to do this? As you're not going to do anything else in parallel, it's pointless.
Why use a timer? Essentially the same argument applies here too as with threads: you're not doing anything else in parallel, so what's the point? And putting a timer inside a thread seems doubly pointless.


Maybe you assumed that by running this in a thread, the application will just schedule the task and exit. That is not the case. The JVM will continue running until the task is completed, and the program will stay in the foreground. You would have to background the process in the calling shell.

As such, you could replace this with a much simpler implementation:

public static void main(String[] args) throws InterruptedException, IOException {
    if (args.length &lt; 2) {
        System.out.println("usage: java PCShutdown HOURS MINUTES");
    }
    int hours = Integer.parseInt(args[0]);
    int minutes = Integer.parseInt(args[1]);
    Thread.sleep(hours * 60 * 60 * 1000 + minutes * 60 * 1000);
    Runtime.getRuntime().exec("shutdown -s -t 30");
}


But then, I'd return to my earlier statement: Java is overkill for this, use the shell and native scheduling features instead (for example at in UNIX).