4

I want to execute dos based external command through java program if there there is any way please help me

1
  • 6
    Java runs on DOS? Commented Jul 30, 2010 at 11:40

2 Answers 2

6
    String[] options = new String[]{"option1", "option2"};
    Runtime.getRuntime().exec("command", options);
Sign up to request clarification or add additional context in comments.

Comments

2

The following bit of code will run the dir command, and then print out for you as well as error. Taken and adapted from (http://www.devdaily.com/java/edu/pj/pj010016)

import java.io.*;

public class JavaRunCommand {

    public static void main(String args[]) {

        String s = null;

        try {

        // run the Windows command (dir)
            // using the Runtime exec method:
            Process p = Runtime.getRuntime().exec("dir");

            BufferedReader stdInput = new BufferedReader(new 
                 InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new 
                 InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

            // read any errors from the attempted command
            System.out.println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }

            System.exit(0);
        }
        catch (IOException e) {
            System.out.println("exception happened
- here's what I know: ");
            e.printStackTrace();
            System.exit(-1);
        }
    } }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.