Thursday, November 10, 2011

How to execute a DOS command from a different working directory using java?

At many times we require to execute a DOS command from within a specific directory.
How do we do that? Here is a utility class that I have written for this purpose.
You don't even need to look into the functions to use this.
Simply add the class to your code and call the executeCommand function.

Util.executeCommand("dir", "D:\\Yogesh");

Click to see more

Util.java



package utilties;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Set;

public class Util
{
    public static String executeCommand(String command, String executionDir)
    {
        String steps[]= new String[3];
        steps[0]="cmd.exe";
        steps[1]="/C";
        steps[2]=command ;
        Process proc=null;
        try
        {
            if(executionDir!=null)
            {
                 proc=Runtime.getRuntime().exec(steps, null, new File(executionDir));
            }
            else
            {
                proc=Runtime.getRuntime().exec(steps);
            }
            InputStream stdin = proc.getInputStream();
            InputStreamReader isr = new InputStreamReader(stdin);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            StringBuffer bf = new StringBuffer("");
            while ( (line = br.readLine()) != null)
            {
                    bf.append(line+"\r\n");
            }
            br.close();
            isr.close();
            stdin.close();
            return bf.toString();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return "ERROR";
    }
    public static String executeCommand(String command)
    {
        return executeCommand(command, null);
    }

}

No comments:

Post a Comment