Как выполнить команду DOS с параметрами в Java - PullRequest
4 голосов
/ 30 июля 2010

Я хочу выполнить внешнюю команду на основе DOS через Java-программу, если есть какой-либо способ, пожалуйста, помогите мне

Ответы [ 2 ]

6 голосов
/ 30 июля 2010
    String[] options = new String[]{"option1", "option2"};
    Runtime.getRuntime().exec("command", options);
2 голосов
/ 30 июля 2010

Следующий бит кода запустит команду dir, а затем выведет для вас сообщение об ошибке. Взято и адаптировано из (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);
        }
    } }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...