views:

192

answers:

4

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

A: 

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
A: 

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.

Kevin Boyd
+3  A: 

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
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
+1  A: 

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
why the hate for this answer? seems thoughtful and helpful. At least comment when you -1.
Trevor Harrison
after reading the blog entry, +1 from me.
Trevor Harrison
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
@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
@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
point taken. Well, there's always JNI
Jherico