java小难题
来源:百度知道 编辑:UC知道 时间:2024/05/14 09:43:56
用JAVA怎么实现关机和重启?高手赐教
//用于关机的类 (未测试)
public class Shutdown {
//执行命令。传入命令(file)和它的参数(params)
private static void runCommand(String file, String params) {
ParamArray pa = params.split(" ");
Runtime.exec(file, pa); //调用Runtime.exec来执行命令
}
//Windows 老版本下的关机命令
public static void shutdownWinpreXP() {
runCommand("RUNDLL32.EXE", "user32,ExitWindowsEx");
}
//Windows XP 下的关机命令
public static void shutdownWinXP() {
runCommand("shutdown.exe", "-s -t 01");
}
//Linux 下的关机命令
public static void shutdownLinux() {
runCommand("shutdown", "now");
}
//调用这个函数来实现关机
public static void shutdownWindows() {
try {
shutdownWinXP();
catch (IOException e) {
shutdownWinpreXP();
}
}
}
在linux环境的话需要给root权限
关注
通过