Is there command to use on windows from java to make the computer sleep?
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.
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)
.
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.
rundll32
. So answer: there is no proper way
. Right? –
Sting 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 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.
A bit more complicated method but you could use ProcessBuilder
to make a shell script (.sh
or .bat
) and execute that.
© 2022 - 2024 — McMap. All rights reserved.