从 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:04:52  来源:igfitidea点击:

Sleep Windows from Java

javawindowssleepstandby

提问by Hamza Yerlikaya

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

是否有命令可用于从 java 在 windows 上使计算机进入睡眠状态?

回答by Jherico

You can do it by executing a shell command, if you java app has enough rights to do so. The command is...

如果您的 Java 应用程序有足够的权限,您可以通过执行 shell 命令来实现。命令是...

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

That and other commands are shown here.

此处显示该命令和其他命令。

回答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.

但我没试过。

回答by Kevin Boyd

You may want to look at the OnNow/ACPI Support here.

您可能想在此处查看 OnNow/ACPI 支持。

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.

还有一个旧的 SO 帖子在这里讨论了它可能与您想要的相反。不过可能会给你一些线索。