5

Is there command to use on windows from java to make the computer sleep?

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
Hamza Yerlikaya
  • 49,047
  • 44
  • 147
  • 241

6 Answers6

10

You can do it by executing a shell command, if you java app has enough rights to do so. The command is...

Runtime.getRuntime().exec("Rundll32.exe powrprof.dll,SetSuspendState Sleep");

That and other commands are shown here.

Jherico
  • 28,584
  • 8
  • 61
  • 87
  • 1
    even though the OP accepted this as the answer, future readers should look at http://stackoverflow.com/questions/1369122/sleep-windows-from-java/1369832#1369832 also. – Trevor Harrison Sep 03 '09 at 15:11
8

I currently solved this using https://github.com/twall/jna. Information about the call from http://www.pinvoke.net/default.aspx/powrprof.SetSuspendState

import com.sun.jna.Native;
import com.sun.jna.Platform;
public class WindowsSuspend {
  public static native boolean SetSuspendState(boolean hibernate, boolean forceCritical, boolean disableWakeEvent);

  static {
    if (Platform.isWindows())
      Native.register("powrprof");
  }
}

Call it than with WindowsSuspend.SetSuspendState(false, false, false).

hobB1T
  • 81
  • 1
  • 1
5

Anyone suggesting rundll32 should be shot, very few functions are designed to be called by rundll32 and SetSuspendState is not one of them. You will get random behavior (Hibernate vs Standby and Forced vs not forced etc) See this blog entry for more details.

Anders
  • 97,548
  • 12
  • 110
  • 164
  • 1
    why the hate for this answer? seems thoughtful and helpful. At least comment when you -1. – Trevor Harrison Sep 02 '09 at 20:12
  • OK. So the RunDLL32 process ends up with a corrupted stack after calling the function. Who cares? It can't 'infect' the parent process and by the time it occurs the function has already been called. – Jherico Sep 02 '09 at 20:55
  • @Jherico: no, that's not the problem, the problem is, you will be calling SetSuspendState with UNKNOWN arguments, and since SetSuspendState will hibernate OR standby depending on the arguments, that's a bit of a problem – Anders Sep 02 '09 at 21:28
  • @Jherico: Also, if you look at the article you posted, you will notice that they tell you to disable/enable hibernation systemwide to "cure" the unknown/random arguments problem – Anders Sep 02 '09 at 21:32
  • Read blog entry. Don't get it. What is proper way? – turbanoff May 06 '18 at 22:03
  • @turbanoff Don't use rundll32 on functions not designed for rundll32 usage. – Anders May 06 '18 at 22:15
  • @Anders but it doesn't answer original question. All other answers suggest to use `rundll32`. So answer: `there is no proper way`. Right? – turbanoff May 06 '18 at 22:19
  • @turbanoff The proper way is to call `SetSuspendState` from a real programming language that follows the Windows ABI. For Java that means JNI if the function is not available in the language itself. – Anders May 06 '18 at 23:10
0

No. You'd need to execute a separate binary via Runtime.exec().

This article suggests

rundll32 Powrprof.dll,SetSuspendState 

but I've not tried it.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

You may want to look at the OnNow/ACPI Support here.

There is also an old SO post that talks about it here. Probably the reverse of what you want. Might give you some clues though.

Community
  • 1
  • 1
Kevin Boyd
  • 12,121
  • 28
  • 86
  • 128
0

A bit more complicated method but you could use ProcessBuilder to make a shell script (.sh or .bat) and execute that.

Tyler Moen
  • 81
  • 5