Sleep Windows from Java
Asked Answered
J

6

5

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

Jasen answered 2/9, 2009 at 17:35 Comment(1)
Removed the hibernate tag as it refers to the ORM.Screens
R
9

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.

Ronni answered 2/9, 2009 at 17:48 Comment(1)
even though the OP accepted this as the answer, future readers should look at https://mcmap.net/q/1863535/-sleep-windows-from-java/… also.Quincy
U
9

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).

Upgrowth answered 12/2, 2013 at 18:46 Comment(0)
D
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.

Dyche answered 2/9, 2009 at 20:5 Comment(8)
why the hate for this answer? seems thoughtful and helpful. At least comment when you -1.Quincy
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.Ronni
@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 problemDyche
@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 problemDyche
Read blog entry. Don't get it. What is proper way?Sting
@Sting Don't use rundll32 on functions not designed for rundll32 usage.Dyche
@Dyche but it doesn't answer original question. All other answers suggest to use rundll32. So answer: there is no proper way. Right?Sting
@Sting 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.Dyche
C
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.

Coreencorel answered 2/9, 2009 at 17:46 Comment(0)
D
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.

Dredger answered 2/9, 2009 at 17:47 Comment(0)
S
0

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

Supraliminal answered 4/8, 2020 at 20:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.