繁体   English   中英

关闭电脑

[英]Shutting down a computer

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

创建自己的函数以通过命令行执行OS 命令

为了举例。 但是要知道,在其他地方以及为什么要使用它。

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

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

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

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

这是使用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;
}

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

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

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

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

最好使用.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();
}

做工不错

您可以使用JNI来完成C / C ++的任何操作。

默认情况下,在Windows Embedded中,cmd中没有shutdown命令。 在这种情况下,您需要手动添加此命令,或者通过使用JNA(如果需要更多Java)或JNI(如果更方便,可以在C代码中设置特权)使用win32(user32.lib)中的ExitWindowsEx函数。

简易单行

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

但仅适用于Windows

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM