71

Is there a way to shutdown a computer using a built-in Java method?

Hrqls
  • 2,944
  • 4
  • 34
  • 54
jjnguy
  • 136,852
  • 53
  • 295
  • 323

9 Answers9

95

Create your own function to execute an OS command through the command line?

For the sake of an example. But know where and why you'd want to use this as others note.

public static void main(String arg[]) throws IOException{
    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec("shutdown -s -t 0");
    System.exit(0);
}
loggeek
  • 123
  • 1
  • 9
David McGraw
  • 5,157
  • 7
  • 36
  • 36
85

Here's another example that could work cross-platform:

public static void shutdown() throws RuntimeException, IOException {
    String shutdownCommand;
    String operatingSystem = System.getProperty("os.name");

    if ("Linux".equals(operatingSystem) || "Mac OS X".equals(operatingSystem)) {
        shutdownCommand = "shutdown -h now";
    }
    // This will work on any version of windows including version 11 
    else if (operatingSystem.contains("Windows")) {
        shutdownCommand = "shutdown.exe -s -t 0";
    }
    else {
        throw new RuntimeException("Unsupported operating system.");
    }

    Runtime.getRuntime().exec(shutdownCommand);
    System.exit(0);
}

The specific shutdown commands may require different paths or administrative privileges.

Suraj Shingade
  • 2,134
  • 4
  • 17
  • 24
David Crow
  • 16,077
  • 8
  • 42
  • 34
  • My only problem with this is the future. What if another type of operating system comes out and someone makes a JVM for that? – Ky - Jan 22 '12 at 06:24
  • 25
    then you add the new condition... software is never finished. – Gubatron Aug 20 '14 at 14:34
38

Here is an example using Apache Commons Lang's SystemUtils:

public static boolean shutdown(int time) throws IOException {
    String shutdownCommand = null, t = time == 0 ? "now" : String.valueOf(time);

    if(SystemUtils.IS_OS_AIX)
        shutdownCommand = "shutdown -Fh " + t;
    else if(SystemUtils.IS_OS_FREE_BSD || SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC|| SystemUtils.IS_OS_MAC_OSX || SystemUtils.IS_OS_NET_BSD || SystemUtils.IS_OS_OPEN_BSD || SystemUtils.IS_OS_UNIX)
        shutdownCommand = "shutdown -h " + t;
    else if(SystemUtils.IS_OS_HP_UX)
        shutdownCommand = "shutdown -hy " + t;
    else if(SystemUtils.IS_OS_IRIX)
        shutdownCommand = "shutdown -y -g " + t;
    else if(SystemUtils.IS_OS_SOLARIS || SystemUtils.IS_OS_SUN_OS)
        shutdownCommand = "shutdown -y -i5 -g" + t;
    else if(SystemUtils.IS_OS_WINDOWS)
        shutdownCommand = "shutdown.exe /s /t " + t;
    else
        return false;

    Runtime.getRuntime().exec(shutdownCommand);
    return true;
}

This method takes into account a whole lot more operating systems than any of the above answers. It also looks a lot nicer and is more reliable then checking the os.name property.

Edit: Supports delay and all versions of Windows (inc. 8/10).

Kezz
  • 1,680
  • 3
  • 19
  • 37
  • Thank you very much this is very useful! Do you know of any similar way to sleep a computer in every OS? – JFreeman Jan 05 '18 at 02:15
  • 1
    @JFreeman Sorry for late reply, but for future people seeking this out, you could just change the commands to whatever the OS command for sleeping is. It now includes Windows 8+ support. – Kezz Feb 27 '19 at 15:44
  • In the windows branch of the condition, you must multiply the t value * 60 as Windows shutdown is measured in seconds (vs minutes in the rest of the *nix OS). – Luis U. May 20 '20 at 13:53
22

The quick answer is no. The only way to do it is by invoking the OS-specific commands that will cause the computer to shutdown, assuming your application has the necessary privileges to do it. This is inherently non-portable, so you'd need either to know where your application will run or have different methods for different OSs and detect which one to use.

Wilson
  • 751
  • 1
  • 7
  • 9
8

I use this program to shutdown the computer in X minutes.

   public class Shutdown {
    public static void main(String[] args) {

        int minutes = Integer.valueOf(args[0]);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                ProcessBuilder processBuilder = new ProcessBuilder("shutdown",
                        "/s");
                try {
                    processBuilder.start();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }, minutes * 60 * 1000);

        System.out.println(" Shutting down in " + minutes + " minutes");
    }
 }
Jonathan Barbero
  • 2,504
  • 1
  • 26
  • 47
7

Better use .startsWith than use .equals ...

String osName = System.getProperty("os.name");        
if (osName.startsWith("Win")) {
  shutdownCommand = "shutdown.exe -s -t 0";
} else if (osName.startsWith("Linux") || osName.startsWith("Mac")) {
  shutdownCommand = "shutdown -h now";
} else {
  System.err.println("Shutdown unsupported operating system ...");
    //closeApp();
}

work fine

Ra.

Ra.
  • 71
  • 1
  • 1
2

You can use JNI to do it in whatever way you'd do it with C/C++.

Steve M
  • 10,517
  • 12
  • 52
  • 63
1

On Windows Embedded by default there is no shutdown command in cmd. In such case you need add this command manually or use function ExitWindowsEx from win32 (user32.lib) by using JNA (if you want more Java) or JNI (if easier for you will be to set priviliges in C code).

Waldemar Wosiński
  • 1,490
  • 17
  • 28
1

easy single line

Runtime.getRuntime().exec("shutdown -s -t 0");

but only work on windows

Andrea Bori
  • 190
  • 2
  • 15