Labels

Saturday 14 September 2013

Shutdown PC by Java Code

class Shutdown
{
public static void main(String args[]) throws Exception
{
// Create Runtime object
Runtime r=Runtime.getRuntime();

// Shutdown system
r.exec("shutdown -s");

// Restart system
r.exec("shutdown -r");

// Shutdown after specific time (here 60 seconds)
r.exec("shutdown -s -t 60");

// Restart after specific time (here 60 seconds)
r.exec("shutdown -r -t 60");

}
}

I think you're a bit happy now.Fine, you'll have to note that the maximum time that you can make your PC wait for shutdown is 10 years, if you want more than that..... nothing, simply go send a feedback to Microsoft about it, they can do if they wish to, but have a valid reason for the claim ;)

More..

r.exec("shutdown -l"); for logging off, you can also add timer for it, as usual. If you wish to abort system being shutdown then, r.exec("shutdown -a"); this prevents your system from being shutdown.

To turn off PC without warning, then r.exec("shutdown -s -p");

To hibernate a PC, then r.exec("shutdown -h"); you can also use timer for this, not only for this, for any shutdown related operation you want to perform, you can use this option.
- See more at: http://java-demos.blogspot.com/2012/10/shutdown-system-in-java.html#sthash.nEFgIvTm.dpuf

http://java-demos.blogspot.com/2012/10/shutdown-system-in-java.html


Cross Plattom

public static void shutdown() throws RuntimeException, IOException {    
String shutdownCommand; String operatingSystem = System.getProperty("os.name");
if ("Linux".equals(operatingSystem) || "Mac OS X".equals(operatingSystem)) {
shutdownCommand = "shutdown -h now"; }    
else if ("Windows".equals(operatingSystem)){     shutdownCommand = "shutdown.exe -s -t 0"; }    
else { throw new RuntimeException("Unsupported operating system."); }
Runtime.getRuntime().exec(shutdownCommand);    
System.exit(0); }

source: http://stackoverflow.com/questions/25637/shutting-down-a-computer-using-java

No comments:

Post a Comment