closures电脑

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

创build自己的函数来通过命令行执行操作系统命令 ?

为了一个例子。 但是要知道你在哪里以及为什么要像其他人那样使用它。

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

特定的关机命令可能需要不同的path或pipe理权限。

这是一个使用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_XP || SystemUtils.IS_OS_WINDOWS_VISTA || SystemUtils.IS_OS_WINDOWS_7) shutdownCommand = "shutdown.exe -s -t " + t; else return false; Runtime.getRuntime().exec(shutdownCommand); return true; } 

这种方法考虑到了比以上任何一个答案都多的操作系统。 它也看起来好多了,更可靠,然后检查os.name属性。

编辑:它现在有用户input延迟,如果他们喜欢的选项!

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

我用这个程序在X分钟内closures电脑。

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

工作正常

镭。

你可以用任何你用C / C ++做的方式来使用JNI 。

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

简单的单线

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

但只能在windows上工作