tags:

views:

215

answers:

6

how do i shut down my OS with java

+1  A: 

You can execute shutdown DOS command on XP and higher

ArsenMkrt
+2  A: 

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

DroidIn.net
+5  A: 

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

Carlos Muñoz
Java could provide an OS independent method for shutting down the machine.
Tom Hawtin - tackline
But it doesn't. Which is good :)
Thorbjørn Ravn Andersen
A: 

This page some useful information.... it does not have a direct answer but it will help you.

Shoban
+3  A: 

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

}
Ric Tokyo
But this is OS dependent!!
Rakesh Juyal
Yes, just read the code and you can update what goes in the runtime.exec so that it can be a Linux command or any other OS command.. Linux "shutdown -h now" or "init 0" ..
Ric Tokyo
+1  A: 
// 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) {}

:-)

paxdiablo