Execute cmd commands from Java

Asked

Viewed 9,291 times

8

I wonder how I do to execute cmd commands in java.
Well, I did that:

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 certainly!
More like I do to run more cmd commands and show them to the user?

  • You want to run one command at a time or you want to run them all at the same time?

1 answer

6


As the answer is in Soen you can pick up the output from 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 run multiple commands at the same time in CMD you can use the & (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 it 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 in execultar start actually cmd gets caught and so you don’t get the answers, one way you can test is to use the cmd /c just, I made 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 don’t know if you’re using any IDE to make it easy, but if you’re going to compile manually, do this:

javac Commands.java
java Commands ConsoleTest

The parameter ConsoleTest is to catch the exits of the System.out.println

  • It didn’t work! He’s just opening cmd, no longer executing commands...

  • @Dongabi tested the last example he uses ProcessBuilder and it worked, I tested please.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.