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(); } }
This blog is primarily focus on Java fundamentals and the libraries built on top of Java programming language. Most of the post are example oriented, hope you have fun in reading my blog....:)
Wednesday, 9 April 2014
Shutting down a Computer using Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment