<p>Is there a way to shutdown a computer using a built-in Java method?</p>

[{'answer_id': 25644, 'author': 'David McGraw', 'author_id': 568, 'author_profile': 'https://Stackoverflow.com/users/568', 'pm_score': 8, 'selected': True, 'text': '<p>Create your own function to execute an OS command through the <a href="http://www.computerhope.com/shutdown.htm" rel="nofollow noreferrer">command line</a>?</p>\n<p>For the sake of an example. But know where and why you\'d want to use this as others note.</p>\n<pre><code>public static void main(String arg[]) throws IOException{\n    Runtime runtime = Runtime.getRuntime();\n    Process proc = runtime.exec(&quot;shutdown -s -t 0&quot;);\n    System.exit(0);\n}\n</code></pre>\n'}, {'answer_id': 25648, 'author': 'Wilson', 'author_id': 107, 'author_profile': 'https://Stackoverflow.com/users/107', 'pm_score': 5, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 25650, 'author': 'Steve M', 'author_id': 1693, 'author_profile': 'https://Stackoverflow.com/users/1693', 'pm_score': 2, 'selected': False, 'text': '<p>You can use <a href="http://en.wikipedia.org/wiki/Java_Native_Interface" rel="nofollow noreferrer">JNI</a> to do it in whatever way you\'d do it with C/C++.</p>\n'}, {'answer_id': 25666, 'author': 'David Crow', 'author_id': 2783, 'author_profile': 'https://Stackoverflow.com/users/2783', 'pm_score': 6, 'selected': False, 'text': "<p>Here's another example that could work cross-platform:</p>\n<pre><code>public static void shutdown() throws RuntimeException, IOException {\n    String shutdownCommand;\n    String operatingSystem = System.getProperty(&quot;os.name&quot;);\n\n    if (&quot;Linux&quot;.equals(operatingSystem) || &quot;Mac OS X&quot;.equals(operatingSystem)) {\n        shutdownCommand = &quot;shutdown -h now&quot;;\n    }\n    // This will work on any version of windows including version 11 \n    else if (operatingSystem.contains(&quot;Windows&quot;)) {\n        shutdownCommand = &quot;shutdown.exe -s -t 0&quot;;\n    }\n    else {\n        throw new RuntimeException(&quot;Unsupported operating system.&quot;);\n    }\n\n    Runtime.getRuntime().exec(shutdownCommand);\n    System.exit(0);\n}\n</code></pre>\n<p>The specific shutdown commands may require different paths or administrative privileges.</p>\n"}, {'answer_id': 2153270, 'author': 'Ra.', 'author_id': 260787, 'author_profile': 'https://Stackoverflow.com/users/260787', 'pm_score': 3, 'selected': False, 'text': '<p>Better use .startsWith than use .equals ... </p>\n\n<pre><code>String osName = System.getProperty("os.name");        \nif (osName.startsWith("Win")) {\n  shutdownCommand = "shutdown.exe -s -t 0";\n} else if (osName.startsWith("Linux") || osName.startsWith("Mac")) {\n  shutdownCommand = "shutdown -h now";\n} else {\n  System.err.println("Shutdown unsupported operating system ...");\n    //closeApp();\n}\n</code></pre>\n\n<p>work fine</p>\n\n<p>Ra.</p>\n'}, {'answer_id': 2213729, 'author': 'Jonathan Barbero', 'author_id': 14811, 'author_profile': 'https://Stackoverflow.com/users/14811', 'pm_score': 3, 'selected': False, 'text': '<p>I use this program to shutdown the computer in X minutes.</p>\n\n<pre><code>   public class Shutdown {\n    public static void main(String[] args) {\n\n        int minutes = Integer.valueOf(args[0]);\n        Timer timer = new Timer();\n        timer.schedule(new TimerTask() {\n\n            @Override\n            public void run() {\n                ProcessBuilder processBuilder = new ProcessBuilder("shutdown",\n                        "/s");\n                try {\n                    processBuilder.start();\n                } catch (IOException e) {\n                    throw new RuntimeException(e);\n                }\n            }\n\n        }, minutes * 60 * 1000);\n\n        System.out.println(" Shutting down in " + minutes + " minutes");\n    }\n }\n</code></pre>\n'}, {'answer_id': 10829952, 'author': 'Waldemar Wosiński', 'author_id': 1255181, 'author_profile': 'https://Stackoverflow.com/users/1255181', 'pm_score': 1, 'selected': False, 'text': '<p>On Windows Embedded by default there is no shutdown command in cmd.\nIn 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).</p>\n'}, {'answer_id': 14297352, 'author': 'Kezz', 'author_id': 1590990, 'author_profile': 'https://Stackoverflow.com/users/1590990', 'pm_score': 5, 'selected': False, 'text': '<p>Here is an example using <a href="http://commons.apache.org/proper/commons-lang/" rel="noreferrer">Apache Commons Lang\'s</a> <a href="http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/SystemUtils.html" rel="noreferrer">SystemUtils</a>:</p>\n\n<pre><code>public static boolean shutdown(int time) throws IOException {\n    String shutdownCommand = null, t = time == 0 ? "now" : String.valueOf(time);\n\n    if(SystemUtils.IS_OS_AIX)\n        shutdownCommand = "shutdown -Fh " + t;\n    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)\n        shutdownCommand = "shutdown -h " + t;\n    else if(SystemUtils.IS_OS_HP_UX)\n        shutdownCommand = "shutdown -hy " + t;\n    else if(SystemUtils.IS_OS_IRIX)\n        shutdownCommand = "shutdown -y -g " + t;\n    else if(SystemUtils.IS_OS_SOLARIS || SystemUtils.IS_OS_SUN_OS)\n        shutdownCommand = "shutdown -y -i5 -g" + t;\n    else if(SystemUtils.IS_OS_WINDOWS)\n        shutdownCommand = "shutdown.exe /s /t " + t;\n    else\n        return false;\n\n    Runtime.getRuntime().exec(shutdownCommand);\n    return true;\n}\n</code></pre>\n\n<p>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 <code>os.name</code> property.</p>\n\n<p><strong>Edit:</strong> Supports delay and all versions of Windows (inc. 8/10).</p>\n'}, {'answer_id': 26782022, 'author': 'Andrea Bori', 'author_id': 4216184, 'author_profile': 'https://Stackoverflow.com/users/4216184', 'pm_score': 1, 'selected': False, 'text': '<p>easy single line</p>\n\n<pre><code>Runtime.getRuntime().exec("shutdown -s -t 0");\n</code></pre>\n\n<p>but only work on windows</p>\n'}]
['https://Stackoverflow.com/questions/25637', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2598/']