Sleep Windows with Java

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

+2


source to share


5 answers


You can do this by executing a shell command if the java application has sufficient rights to do so. Command...

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

      



This and other commands are shown here .

+9


source


Anyone suggesting rundll32 should be shot, there are very few functions designed to call rundll32 and SetSuspendState is n't . You will get random behavior (Hibernate vs Standby and Forced vs not force, etc.). See this blog post for details .



+5


source


I currently solved this with https://github.com/twall/jna . Call info 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");
  }
}

      

Name it instead WindowsSuspend.SetSuspendState(false, false, false)

.

+5


source


Not. You will need to execute seperate binary through Runtime.exec()

.

This article suggests

rundll32 Powrprof.dll,SetSuspendState 

      

but I haven't tried it.

0


source


You may need to look at OnNow / ACPI support here .

There is also an old SO post that talks about this here. Probably the opposite is what you want. Can give you some hints though.

0


source







All Articles