->Execute DOS Commands using Java Program

Executing the DOS commands using java program is very easy, here is how it goes.

We have Runtime object that allows to the application to interface with the environment in which the application is running. So we can execute these DOS commands with this object.

You can use “Runtime.getRuntime().exec(dosCommand)” to execute DOS command, but it fails. You can’t run DOS commands directly by specifying Runtime.getRuntime().exec(dosCommand) in Java. Instead, to execute a DOS command (such as DIR, MKDIR, RD) from a Java program, you need to add the command to run the Windows command shell cmd /c to the command you want to execute. The /c switch terminates the command shell after the desired command completes. Here’s the syntax:

Runtime.getRuntime().exec(“cmd /c ” + dosCommand);
Here is an Example that creates a Directory in C drive.

package com.cmd;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CommandScrips {
       public static void main(String[] args) {
       try {
              String dosCommand = "cmd /c mkdir C:\\ddd ";
             Process p = Runtime.getRuntime().exec("cmd /c "+dosCommand);
             BufferedReader stdInput = new BufferedReader(new
             InputStreamReader(p.getInputStream()));
             BufferedReader stdError = new BufferedReader(new
             InputStreamReader(p.getErrorStream()));
             String s = null;
              System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
               }
             System.out.println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                  System.err.println(s);
            }
       } catch (IOException e) {
// TODO Auto-generated catch block
               e.printStackTrace();
       }
    }
}

And here’s an example that gets the file list in a directory and its sub directories using the DIR command with the /s (include sub directories) switch:

      final String dosCommand = "cmd /c dir /s";

      final String location = "C:\\WINDOWS"; //from this folder it displays all files and folders

      try {

         final Process process = Runtime.getRuntime().exec(dosCommand + " " + location);

         final InputStream in = process.getInputStream();

         int ch;

         while((ch = in.read()) != -1) {

            System.out.print((char)ch);

         }

      } catch (IOException e) {

         e.printStackTrace();

      }
Advertisements

1 Comment

  1. Prafulla said,

    January 7, 2013 at 10:35 AM

    Yes Man it is good example but why you repeating “cmd /c” this cmd , without repeating it You can do this.


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: