开发者

How do I shut down my OS with Java

How do I shut down my OS with J开发者_运维技巧ava?


I believe that since Java is OS independent there is no direct way to do it, other than invoking something from the underlying OS.


from : a blog

package com.deepak.entertainment;

import java.io.IOException;
import java.io.OutputStream;

public class Deepak {

 public static void main(String[] args) throws InterruptedException {
  Runtime runtime = Runtime.getRuntime();
  try {
   Process process = runtime.exec(”C:\\WINDOWS\\system32\\cmd.exe”);
   OutputStream os = process.getOutputStream();
   os.write(”shutdown -s -f -t 90 \n\r”.getBytes());
   os.close();
   process.waitFor();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

}


You cannot directly shut OS with Java, but you can execute a shell script or native program e.g Runtime.getRuntime().exec("shutdown"). Or you can write JNI hook into native system


You can execute shutdown DOS command on XP and higher


// May need to change for a Swing or SWT application.
System.out.println ("Please shut down your OS now. I'll wait...");
boolean forever = true;
while (forever) {}

:-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜