Java 关闭计算机

Java 关闭计算机,java,cross-platform,shutdown,Java,Cross Platform,Shutdown,有没有一种方法可以使用内置Java方法关闭计算机?创建自己的函数以通过执行操作系统命令 为了举例说明。但是要知道你在哪里以及为什么要像其他人所说的那样使用它 public static void main(String arg[]) throws IOException{ Runtime runtime = Runtime.getRuntime(); Process proc = runtime.exec("shutdown -s -t 0"); Sy

有没有一种方法可以使用内置Java方法关闭计算机?

创建自己的函数以通过执行操作系统命令

为了举例说明。但是要知道你在哪里以及为什么要像其他人所说的那样使用它

public static void main(String arg[]) throws IOException{
    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec("shutdown -s -t 0");
    System.exit(0);
}

答案是否定的。唯一的方法是调用操作系统特定的命令,这将导致计算机关闭,前提是您的应用程序具有执行此操作所需的权限。这本质上是不可移植的,因此您需要知道应用程序将在何处运行,或者对不同的操作系统使用不同的方法,并检测要使用哪种方法。

您可以使用C/C++以任何方式进行操作。

下面是另一个可以跨平台工作的示例:

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);
}

特定的关机命令可能需要不同的路径或管理权限。

更好地使用.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();
}
干得好


Ra.

我使用此程序在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");
    }
 }

默认情况下,在嵌入式Windows上,cmd中没有shutdown命令。 在这种情况下,您需要手动添加此命令,或使用JNA(如果您需要更多Java)或JNI(如果您更容易在C代码中设置特权),从win32(user32.lib)中使用函数ExitWindowsEx。

下面是一个使用:

这种方法考虑的操作系统比上述任何答案都要多得多。与检查
os.name
属性相比,它看起来更漂亮、更可靠

编辑:支持延迟和所有版本的Windows(包括8/10)。

轻松单行

Runtime.getRuntime().exec("shutdown -s -t 0");

但是只在windows上工作

,直到有人使用一个名为Macaroni的新操作系统,其中shutdown是自毁命令;答案中的第一个链接已经过时。我唯一的问题是未来。如果出现了另一种类型的操作系统,并且有人为此制作了一个JVM呢?然后您添加了新的条件。。。软件永远不会完成。非常感谢这是非常有用的!你知道在每个操作系统中都有类似的方式让电脑睡眠吗?@JFreeman很抱歉没有及时回复,但对于将来想了解这一点的人,你可以将命令更改为任何用于睡眠的操作系统命令。它现在包括Windows 8+支持。在条件的Windows分支中,当Windows关机以秒为单位测量时,您必须乘以t值*60(在其余的*nix操作系统中为分钟)。
Runtime.getRuntime().exec("shutdown -s -t 0");