Java 关闭计算机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25637/
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
Shutting down a computer
提问by jjnguy
Is there a way to shutdown a computer using a built-in Java method?
有没有办法使用内置的 Java 方法关闭计算机?
采纳答案by David McGraw
Create your own function to execute an OS commandthrough the command line?
For the sake of an example. But know where and why you'd want to use this as others note.
为了一个例子。但是,正如其他人所指出的那样,知道在哪里以及为什么要使用它。
public static void main(String arg[]) throws IOException{
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("shutdown -s -t 0");
System.exit(0);
}
回答by Wilson
The quick answer is no. The only way to do it is by invoking the OS-specific commands that will cause the computer to shutdown, assuming your application has the necessary privileges to do it. This is inherently non-portable, so you'd need either to know where your application will run or have different methods for different OSs and detect which one to use.
快速的答案是否定的。唯一的方法是调用特定于操作系统的命令,这些命令将导致计算机关闭,假设您的应用程序具有执行此操作所需的权限。这本质上是不可移植的,因此您要么需要知道您的应用程序将在哪里运行,要么需要针对不同的操作系统使用不同的方法并检测要使用的方法。
回答by Steve M
回答by David Crow
Here's another example that could work cross-platform:
这是另一个可以跨平台工作的示例:
public static void shutdown() throws RuntimeException, IOException {
String shutdownCommand;
String operatingSystem = System.getProperty("os.name");
if ("Linux".equals(operatingSystem) || "Mac OS X".equals(operatingSystem)) {
shutdownCommand = "shutdown -h now";
}
else if ("Windows".equals(operatingSystem)) {
shutdownCommand = "shutdown.exe -s -t 0";
}
else {
throw new RuntimeException("Unsupported operating system.");
}
Runtime.getRuntime().exec(shutdownCommand);
System.exit(0);
}
The specific shutdown commands may require different paths or administrative privileges.
特定的关闭命令可能需要不同的路径或管理权限。
回答by Ra.
Better use .startsWith than use .equals ...
使用 .startsWith 比使用 .equals 更好......
String osName = System.getProperty("os.name");
if (osName.startsWith("Win")) {
shutdownCommand = "shutdown.exe -s -t 0";
} else if (osName.startsWith("Linux") || osName.startsWith("Mac")) {
shutdownCommand = "shutdown -h now";
} else {
System.err.println("Shutdown unsupported operating system ...");
//closeApp();
}
work fine
工作正常
Ra.
镭。
回答by Jonathan Barbero
I use this program to shutdown the computer in X minutes.
我使用这个程序在 X 分钟内关闭计算机。
public class Shutdown {
public static void main(String[] args) {
int minutes = Integer.valueOf(args[0]);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
ProcessBuilder processBuilder = new ProcessBuilder("shutdown",
"/s");
try {
processBuilder.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}, minutes * 60 * 1000);
System.out.println(" Shutting down in " + minutes + " minutes");
}
}
回答by Waldemar Wosiński
On Windows Embedded by default there is no shutdown command in cmd. In such case you need add this command manually or use function ExitWindowsEx from win32 (user32.lib) by using JNA (if you want more Java) or JNI (if easier for you will be to set priviliges in C code).
默认情况下,在 Windows Embedded 上,cmd 中没有关闭命令。在这种情况下,您需要手动添加此命令或通过使用 JNA(如果您需要更多 Java)或 JNI(如果您更容易在 C 代码中设置权限)使用 win32(user32.lib)中的 ExitWindowsEx 函数。
回答by Kezz101
Here is an example using Apache Commons Lang'sSystemUtils:
下面是一个使用Apache Commons Lang 的SystemUtils的例子:
public static boolean shutdown(int time) throws IOException {
String shutdownCommand = null, t = time == 0 ? "now" : String.valueOf(time);
if(SystemUtils.IS_OS_AIX)
shutdownCommand = "shutdown -Fh " + t;
else if(SystemUtils.IS_OS_FREE_BSD || SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC|| SystemUtils.IS_OS_MAC_OSX || SystemUtils.IS_OS_NET_BSD || SystemUtils.IS_OS_OPEN_BSD || SystemUtils.IS_OS_UNIX)
shutdownCommand = "shutdown -h " + t;
else if(SystemUtils.IS_OS_HP_UX)
shutdownCommand = "shutdown -hy " + t;
else if(SystemUtils.IS_OS_IRIX)
shutdownCommand = "shutdown -y -g " + t;
else if(SystemUtils.IS_OS_SOLARIS || SystemUtils.IS_OS_SUN_OS)
shutdownCommand = "shutdown -y -i5 -g" + t;
else if(SystemUtils.IS_OS_WINDOWS)
shutdownCommand = "shutdown.exe /s /t " + t;
else
return false;
Runtime.getRuntime().exec(shutdownCommand);
return true;
}
This method takes into account a whole lot more operating systems than any of the above answers. It also looks a lot nicer and is more reliable then checking the os.name
property.
这种方法考虑的操作系统比上述任何一个答案都多。它看起来也更好,并且比检查os.name
属性更可靠。
Edit:Supports delay and all versions of Windows (inc. 8/10).
编辑:支持延迟和所有版本的 Windows(包括 8/10)。
回答by Andrea Bori
easy single line
简单的单线
Runtime.getRuntime().exec("shutdown -s -t 0");
but only work on windows
但只适用于 Windows