嗨,我们可以在程序java中放置像reboot这样的终端命令吗? [英] hi.Can we place terminal commands like reboot in the program java?

查看:53
本文介绍了嗨,我们可以在程序java中放置像reboot这样的终端命令吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请回复任何关于这个问题的内容

plz give reply anything what do you know about this question

推荐答案

我们可以将终端命令放在java程序中。



以下是示例代码。它适用于跨平台



We can put terminal command in java program.

Here is the sample code.It works on 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);
}


这篇关于嗨,我们可以在程序java中放置像reboot这样的终端命令吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆