关闭电脑

问题描述 投票:65回答:9

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

java cross-platform shutdown
9个回答
82
投票

创建自己的函数来通过command执行操作系统command line

为了举个例子。但要知道你想要使用它的地点和原因,正如其他人所说。

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

76
投票

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

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

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


27
投票

以下是使用Apache Commons Lang's 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)。


22
投票

快速回答是否定的。唯一的方法是调用特定于操作系统的命令,这些命令将导致计算机关闭,假设您的应用程序具有执行此操作所需的特权。这本质上是不可移植的,因此您需要知道应用程序的运行位置,或者为不同的操作系统使用不同的方法,并检测要使用哪种操作系统。


8
投票

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

5
投票

更好地使用.starts而不是使用.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();
}

工作得很好

出。


2
投票

您可以使用JNI以您使用C / C ++的方式执行此操作。


1
投票

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


1
投票

简单的单行

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

但只适用于Windows

© www.soinside.com 2019 - 2024. All rights reserved.