Execute DOS commands in Java


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.

In Java

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

DOS command
Java application

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.


Runtime.getRuntime().exec(“shutdown -r “);// This will restart the computer.

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


2 thoughts on “Execute DOS commands in Java

  1. sir i have another question for require help you
    now day’s i have create QRCode base attendance system using webcam to read the data QRCode and to give attendance sir i already read and generate qrcode and create webcam to take photo but my problem how to pass webcam image over qrcode to read this image data ……….?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s