Not like in these days, early days people frequently use DOS commands to do certain tasks with computer. New users had to practice MS DOS commands before learn Windows (that’s true for me too). Still if you are interested about Linux you have to deal with keyboard commands. Because Linux users still believe that it’s a powerful way.
Same as we do in command prompt we can execute some MS DOS commands using Java. Just a single line of code.
<br />Runtime.getRuntime().exec("command");<br />
Example : Opening Notepad
Using this method you can execute DOS commands,open an application and so on. Here I’m going to mention about how to Shutdown, Restart, Hibernate and Log off computer using above method. The only thing you have to do is insert the command with suitable additional flags.
SHUTDOWN [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c “comment”] [-d up:xx:yy]
|No args||Display this message (same as -?)|
|-i||Display GUI interface, must be the first option|
|-l||Log off (cannot be used with -m option)|
|-s||Shutdown the computer|
|-r||Shutdown and restart the computer|
|-a||Abort a system shutdown|
|-m \\computername||Remote computer to shutdown/restart/abort|
|-t xx||Set timeout for shutdown to xx seconds|
|-c “comment”||Shutdown comment (maximum of 127 characters)|
|-f||Forces running applications to close without warning|
|-d [u] [p]:xx:yy||The reason code for the shutdown
u is the user code
p is a planned shutdown code
xx is the major reason code (positive integer less than 256)
yy is the minor reason code (positive integer less than 65536)
LOGOFF [sessionname | sessionid] [/SERVER:servername] [/V]
|sessionname||The name of the session.|
|sessionid||The ID of the session.|
|/SERVER:servername||Specifies the Terminal server containing the user session to log off (default is current).|
|/V||Displays information about the actions performed.|
The application I’ve created to implement can be downloaded from here.
(It’ll ask your email. If you send an email with subject “Shutdown” to your account, application will open it and shutdown your computer. Likewise I’ve implemented Restart,Hibernate and Log off commands.)
Hope this will help you to develop more advanced application in Java.
1. DOS commands: http://www.computerhope.com/shutdown.htm and http://www.computerhope.com/logoff.htm
2. Java examples: http://stackoverflow.com/questions/25637/shutting-down-a-computer-using-java