2011-04-26 31 views
0

可能重复:
Shutting down a computer using Java爪哇 - 程序运行后关闭计算机

嗨,

我将运行20次,大约需要3个小时的Java程序这样做。在此之后,我希望我的电脑关机,或者让我以用户身份注销。我可以直接从我的程序执行此操作,即直接在for循环之后执行此操作。

for(int i=0; i<20; i++){ 
//APPLICATION CODE 
} 

//SHUTDOWN OR LOGGOF CODE 

我该怎么做?

谢谢!

回答

2

举个例子,你可以运行系统命令是这样的:

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

你刚才救了我的命! – user726219 2011-04-26 23:08:18

0
public void shutDown() { 
    try { 
     Runtime 
      .getRuntime() 
      .exec(
          "shutdown -s -t 120 -c \"Message telling shutdown has initiliazed. To stop the shutdown.\""); 
    } catch (final IOException e) { 
     e.printStackTrace(); 
    } 

}