Month: April 2011

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: and
2. Java examples:


How to make an application portable


After two consecutive java applications I’ve decided to post about something else. When you are forced to do your work on another computer you won’t get a chance to deal with your favorite fancy applications. Sometimes you might not allowed to install on that  computer. At that time the solution is “Portable Applications”.

Portable Applications

Portable applications are applications that sits on your flash drive and allow you to work with them. You do not need to install them in your computer. Therefore for your ease portable version of many frequently used applications can be downloaded from here. But unfortunately it doesn’t contain all the applications that you need. In here I’m going to introduce some methods that allow you to make an application portable yourself.

When I had to make a certain application portable I searched through internet and found following applications

1. MojoPac
It was a very interesting application. It gives a secure virtual desktop, so we can install any application, games in it. But the main drawback is, it needs administrator privileges to run. Therefore it won’t work with Windows Vista or later operating systems.

2. Ceedo
It’s also a same as MojoPac but it’s not for free.

3. Portable App Creator
It’s a freeware that makes some small applications portable.You can try it from here.

Introducing software

But finally I found an interesting software called VMware ThinApp. It allows you to make your own favourite application portable.  You can have VMware ThinApp from official website for 60 days.

Steps to follow

Here are the steps to do that.

1. Make sure that the application you are going to make portable is not installed on your PC

2. Install VMware ThinApp.

3. Run VMware ThinApp and it will scan your PC.

4. Then you are allowed to install the application you want to make portable.

5. Again VMware ThinApp will run a system scan.

6. Just after few steps ……… That’s all. you can find your portable application in “\bin” folder.

You can also find some software that have been already went through that process 😀


1. More about working with ThinApp :

2. MojoPac :

3. More about portable application creators :