可能的重复:
使用 Java 关闭计算机
我正在制作一个将关闭的个人程序我的计算机在一定时间后或在特定时间/日期。但是,我正在运行多个操作系统,并且希望使用一个简单的 Java 程序来完成此操作。 有没有办法在Java中发送独立于系统的机器关闭请求而不使用任何外部库?我知道你可以使用java.awt.Desktop.getDesktop().browse(new URI("shutdown /s"));
在 Windows 中,但是,我再次希望系统独立。
Possible Duplicate:
Shutting down a computer using Java
I am making a personal program that will shut down my computer after a certain amount of time or at a certain time/date. However, I am running multiple operating systems and want to do this with one simple Java program. Is there any way to send a system-independent machine shutdown request in Java without any using any external libraries? I know you can use java.awt.Desktop.getDesktop().browse(new URI("shutdown /s"));
in Windows, but, again, I want system independence.
发布评论
评论(3)
不,没有。
这超出了 JVM 或标准 Java 类库的范围。
快乐编码。
No. There is not.
This is outside the scope of the JVM or standard Java class library.
Happy coding.
为什么不使用调度程序?所有主要操作系统都支持此类功能(cron、at 等)。可能还有其他因素,例如在现代 Windows (Windows 7 )、Linux 等中发挥作用的权限。
如果您想真正使用某些系统调用,请尝试使用 JNA。这大大简化了特定于平台的访问。
Why not use schedulers? All major operating systems supports such feature(cron, at etc). There may be other factors like permission which comes to play in modern windows (windows 7 ), linux etc.
If you want to really use some system call, Try using JNA. That simplifies platform specific access a lot.
@robjb 给了我最好的解决方案。虽然它对我的口味来说有点太不灵活,但我会起诉它,直到遇到问题。
在上面的示例中,
StringPP
是一个自定义类,它通过上面使用的#containsIgnoreCase
等方法增强了String
的功能。Main.LOG
是我制作和使用的一个日志实用程序。@robjb gave me the best solution. Though it is a little too inflexible for my tastes, I will be suing it until I run into a problem.
In the above example,
StringPP
is a custom class that augments the capabilities of aString
with methods such as the above used#containsIgnoreCase
.Main.LOG
is a logging utility that I made and use.