0

Possible Duplicate:
Shutting down a computer using Java

I am making a personal program that will shut down my computer after a certain amount of time or at a certain time/date. However, I am running multiple operating systems and want to do this with one simple Java program. Is there any way to send a system-independent machine shutdown request in Java without any using any external libraries? I know you can use java.awt.Desktop.getDesktop().browse(new URI("shutdown /s")); in Windows, but, again, I want system independence.

Community
  • 1
  • 1
Ky -
  • 30,724
  • 51
  • 192
  • 308
  • 4
    Take a look at: http://stackoverflow.com/questions/25637/shutting-down-a-computer-using-java – Rob Dec 23 '11 at 05:23
  • When taking that approach (which will be your only option), I suggest directly using `Runtime.exec()` instead of browsing some url on the desktop that looks like a command line. – Greg Hewgill Dec 23 '11 at 05:24
  • The post that uses "operatingSystem = System.getProperty("os.name")", then "Runtime.exec()" based on the result, is absolutely his best bet. – paulsm4 Dec 23 '11 at 05:26
  • `System.exit(0);` shuts down the Java Virtual Machine no matter what the underlying platform is. – emory Dec 23 '11 at 05:38
  • I like the post http://stackoverflow.com/a/25650/348975 best. Admittedly it is not very practical, but it would come the closest to system independence. – emory Dec 23 '11 at 05:49
  • @GregHewgill Thank you! I was trying to think of the exact command, but couldn't find or remember it... – Ky - Dec 24 '11 at 01:24
  • @emory That's not what I'm asking for. – Ky - Dec 24 '11 at 01:24

3 Answers3

1

No. There is not.

This is outside the scope of the JVM or standard Java class library.

Happy coding.

  • I would vote this up or accept this if it explained why. As it stands, it just **sounds** like a short-sighted opinion. Sorry... – Ky - Dec 24 '11 at 01:22
  • @Supuhstar Honestly not sure what more there is to say :) The JDK is a general-purpose library. I do not know of *any* standard library for *any* language that has a system-shutdown command which is an operating-system feature. This includes C as many of the system-calls provided by the operating system (be it the Linux kernel or Windows and WinAPI) are *not* exposed in any way via stdlib. Only a subset of all operating system features are exposed as part of even POSIX API: I couldn't find on the "turn off a computer". –  Dec 25 '11 at 00:14
  • so... you're saying that "java.awt.Toolkit#shutdown()" is too system-specific? Why can't the individual JVMs handle the actual shutdown command, and the Java bytecode still be the same? – Ky - Jan 01 '12 at 08:12
  • @Supuhstar Yes. I am saying precisely that. Please refer to above. You can *hide* system-specific behavior (as with the accepted answer), but it is *outside the scope of a standard library: and especially that of Java, as made clear the developers/designers who created it*. –  Jan 01 '12 at 23:19
  • how is shutting down the system outside the scope, but not taking control of the mouse and keyboard? – Ky - Jan 02 '12 at 06:00
  • 1
    @Supuhstar "This is outside the scope of the JVM **or standard Java class library.**" That is, there is nothing intrinsically part of the JVM that allows it and there is nothing in the standard class library that allows it. AWT/Swing (or whatever UI is used in Android), considering for a moment those are "standard", *use native code* to allow keyboard/mouse access. This answer is correct. –  Sep 07 '12 at 16:59
0

Why not use schedulers? All major operating systems supports such feature(cron, at etc). There may be other factors like permission which comes to play in modern windows (windows 7 ), linux etc.

If you want to really use some system call, Try using JNA. That simplifies platform specific access a lot.

Jayan
  • 18,003
  • 15
  • 89
  • 143
0

@robjb gave me the best solution. Though it is a little too inflexible for my tastes, I will be suing it until I run into a problem.

  String shutdownCommand;
  StringPP operatingSystem = new StringPP(System.getProperty("os.name"));

  if (operatingSystem.containsIgnoreCase("linux") ||
      operatingSystem.containsIgnoreCase("mac") ||
      operatingSystem.containsIgnoreCase("unix"))
  {
    shutdownCommand = "sudo shutdown -h -t 30";
  }
  else if (operatingSystem.containsIgnoreCase("windows"))
  {
    shutdownCommand = "shutdown /s /d P:0:0 /t 30 /c \"Blue Husky Timer 2 is shutting down your system, as you requested. \n"
        + "You have 30 seconds to save and close programs\"";
  }
  else
  {
    throw new UnsupportedOperationException("Unsupported operating system.");
  }

  try
  {
    Runtime.getRuntime().exec(shutdownCommand);
  }
  catch (Throwable t)
  {
    Main.LOG.logThrowable(t);
  }
  System.exit(0);

In the above example, StringPP is a custom class that augments the capabilities of a String with methods such as the above used #containsIgnoreCase. Main.LOG is a logging utility that I made and use.

Ky -
  • 30,724
  • 51
  • 192
  • 308