从 Java 睡眠 Windows
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1369122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sleep Windows from Java
提问by Hamza Yerlikaya
Is there command to use on windows from java to make the computer sleep?
是否有命令可用于从 java 在 windows 上使计算机进入睡眠状态?
回答by Jherico
回答by Anders
Anyone suggesting rundll32 should be shot, very few functions are designed to be called by rundll32 and SetSuspendState is notone of them. You will get random behavior (Hibernate vs Standby and Forced vs not forced etc) See this blog entry for more details.
任何建议rundll32的人都应该被枪杀,很少有函数被设计为由rundll32调用,SetSuspendState不是其中之一。您将获得随机行为(休眠与待机和强制与非强制等)有关更多详细信息,请参阅此博客条目。
回答by hobB1T
I currently solved this using https://github.com/twall/jna. Information about the call from http://www.pinvoke.net/default.aspx/powrprof.SetSuspendState
我目前使用https://github.com/twall/jna解决了这个问题。有关来自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)
.
调用它比WindowsSuspend.SetSuspendState(false, false, false)
。
回答by Brian Agnew
No. You'd need to execute a separate binary via Runtime.exec()
.
不,您需要通过Runtime.exec()
.
This articlesuggests
这篇文章建议
rundll32 Powrprof.dll,SetSuspendState
but I've not tried it.
但我没试过。