Execute cmd commands by Java

5

I would like to know how I do to run cmd commands in java. Well, I did this:

import java.io.*;

public class Commands {

static final Runtime run = Runtime.getRuntime();
static Process pro;
static BufferedReader read;

public static void main(String[] args) {
    String Start = "cmd /c start cmd.exe";

    try {
        pro = run.exec(Start);
        read = new BufferedReader(new InputStreamReader(pro.getInputStream()));
        read.readLine();
    } catch(Exception e) {
        System.err.println(e);
    }
}
}  

And that's right! More like I do to run more commands in cmd and show them to the user?

    
asked by anonymous 06.04.2015 / 04:05

1 answer

4

Depending on the answer in the SOen you can get the output of Process using .getOutputStream() and in it you will add the other commands, the code should look like this:

import java.io.*;

public class Commands
{
    static final Runtime run = Runtime.getRuntime();
    static Process pro;
    static BufferedReader read;

    private static void showFB()
    {
        read = new BufferedReader(new InputStreamReader(pro.getInputStream()));
        System.out.println(read.readLine());
    }

    public static void main(String[] args)
    {
        String Start = "cmd /c start cmd.exe";

        try {
            pro = run.exec(Start);
            showFB();//Mostra as resposta

            OutputStream out = pro.getOutputStream();

            out.write("cd C:/ /r/n".getBytes());
            out.flush();
            showFB();//Mostra as resposta

            out.write("dir /r/n".getBytes());
            showFB();//Mostra as resposta

            out.close();
        } catch(Exception e) {
            System.err.println(e);
        }
    }
}

If you want to execute multiple commands at the same time in CMD you can use & (which is also compatible with other operating systems), for example:

set A=1& set B=2& set C=3& echo A& echo B& echo C& DIR

Use in your code like this:

import java.io.*;
import java.lang.String;

public class Commands
{
    static final Runtime run = Runtime.getRuntime();
    static Process pro;
    static BufferedReader read;

    public static void main(String[] args)
    {
        String[] cmds = {
            "cmd /c start cmd.exe",
            "comando 2",
            "comando 3",
            "comando 4"
        };

        try {
            pro = run.exec(String.join("& ", cmds));

            read = new BufferedReader(new InputStreamReader(pro.getInputStream()));
            read.readLine();
        } catch(Exception e) {
            System.err.println(e);
        }
    }
}

I noticed that while executing start , actually the cmd gets stuck and therefore you do not get the answers, one way you can test is to use cmd /c only, I did an example:

import java.io.*;
import java.lang.String;

public class Commands
{
    static final Runtime run = Runtime.getRuntime();
    static Process pro;
    static BufferedReader read;

    public static void main(String[] args)
    {
        String[] cmds = {
            "echo 2",
            "echo 3",
            "echo 4"
        };

        try {
            ProcessBuilder builder = new ProcessBuilder("cmd", "/c",
                String.join("& ", cmds));

            builder.redirectErrorStream(true);

            Process p = builder.start();

            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;

            while (true) {
                line = r.readLine();
                if (line == null) {
                    break;
                }

                System.out.println(line);
            }
        } catch(Exception e) {
            System.err.println(e);
        }
    }
}

I do not know if you are using some IDE for ease, but if you are compiling manually, do this:

javac Commands.java
java Commands ConsoleTest

The ConsoleTest parameter is to take the output of System.out.println

    
06.04.2015 / 04:18