当前位置:首页> 正文

关于Java:关闭计算机

关于Java:关闭计算机

Shutting down a computer

是否有使用内置Java方法关闭计算机的方法?


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

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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的示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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分钟内关闭了计算机。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   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 ...

1
2
3
4
5
6
7
8
9
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。


您可以使用JNI以与C / C相同的方式进行操作。


单行

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

但仅适用于Windows


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


展开全文阅读

相关内容