Wednesday 9 April 2014

Shutting down a Computer using Java



public class Shutdown {

 public static void runCmd(String cmd) {
  try{
   Process proc=Runtime.getRuntime().exec(cmd);                        
  }
  catch(Exception E){
   System.out.println("Exception caught " + E);
  }          
 }
    
 public static void runWindowsCmd(String cmd){
  String cmd1 = "cmd.exe /c ";
  cmd1 = cmd1 + cmd;
       
  try{
   Process proc=Runtime.getRuntime().exec(cmd1);                     
  }
  catch(Exception E){
   System.out.println("Exception caught " + E);
  }
  
 }
    
 public static void shutdown(){
  String osType =  getOsDetails();
  
  if(osType.contains("windows") || osType.contains("Windows") || osType.contains("WINDOWS")){
   runWindowsCmd("shutdown.exe -s -t 0");
  }
  else{
   runCmd("shutdown -h now");
  }
 }
    
 static public String getOsDetails(){
  return System.getProperty("os.name");
 }
    
 public static void main(String args[])throws Exception{
  Shutdown.shutdown();
 }
}

Home

No comments:

Post a Comment