Computer shutdown

Is there a way to shut down a computer using the built-in Java method?

+60
java cross-platform shutdown
Aug 25 '08 at 4:01
source share
9 answers

Create your own function to execute OS via command line ?

For example. But know where and why you want to use it, as others have noted.

public static void main(String arg[]) throws IOException{ Runtime runtime = Runtime.getRuntime(); Process proc = runtime.exec("shutdown -s -t 0"); System.exit(0); } 
+74
Aug 25 '08 at 4:08
source share

Here is another example that can 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"; } else if ("Windows".equals(operatingSystem)) { shutdownCommand = "shutdown.exe -s -t 0"; } else { throw new RuntimeException("Unsupported operating system."); } Runtime.getRuntime().exec(shutdownCommand); System.exit(0); } 

Specific termination commands may require different paths or administrative privileges.

+69
Aug 25 '08 at 4:47
source share

Here is an example using Apache Commons Lang 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_XP || SystemUtils.IS_OS_WINDOWS_VISTA || SystemUtils.IS_OS_WINDOWS_7) shutdownCommand = "shutdown.exe -s -t " + t; else return false; Runtime.getRuntime().exec(shutdownCommand); return true; } 

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

Edit: Now the user has the option to enter a delay if they like!

+26
Jan 12 '13 at 19:56
source share

The quick answer is no. The only way to do this is to invoke OS-specific commands that cause the computer to stop, provided that your application has the necessary privileges to do so. This is inherently not portable, so you need to either know where your application will work, or use different methods for different operating systems and determine which one to use.

+22
Aug 25 '08 at 4:14
source share

I use this program to turn off 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"); } } 
+8
Feb 06 '10 at 16:13
source share

Better use .startsWith 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(); } 

works fine

Ra.

+5
Jan 28 '10 at 8:55
source share

You can use JNI to do this in any way you would do using C / C ++.

+2
Aug 25 '08 at 4:17
source share

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

+1
May 31 '12 at 8:28
source share

simple single line

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

but only works with windows

0
Nov 06 '14 at 2:34
source share



All Articles