48

In Java, I want to be able to execute a Windows command.

The command in question is netsh. This will enable me to set/reset my IP address.

Note that I do not want to execute a batch file.

Instead of using a batch file, I want to execute such commands directly. Is this possible?


Here is my implemented Solution for Future Reference:

public class JavaRunCommand {
    private static final String CMD = 
        "netsh int ip set address name = \"Local Area Connection\" source = static addr = 192.168.222.3 mask = 255.255.255.0";
    public static void main(String args[]) {

        try {
            // Run "netsh" Windows command
            Process process = Runtime.getRuntime().exec(CMD);

            // Get input streams
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

            // Read command standard output
            String s;
            System.out.println("Standard output: ");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

            // Read command errors
            System.out.println("Standard error: ");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
mre
  • 43,520
  • 33
  • 120
  • 170

5 Answers5

43
Runtime.getRuntime().exec("netsh");

See Runtime Javadoc.

EDIT: A later answer by leet suggests that this process is now deprecated. However, as per the comment by DJViking, this appears not to be the case: Java 8 documentation. The method is not deprecated.

badroit
  • 1,316
  • 15
  • 28
  • 8
    In my environment, I had to prepend the command with "cmd /c" i.e. `Runtime.getRuntime().exec("cmd /c netsh");` – Daniel Jun 27 '17 at 17:57
32

Use ProcessBuilder

ProcessBuilder pb=new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process process=pb.start();
BufferedReader inStreamReader = new BufferedReader(
    new InputStreamReader(process.getInputStream())); 

while(inStreamReader.readLine() != null){
    //do something with commandline output.
}
Daniel
  • 8,655
  • 5
  • 60
  • 87
leet
  • 933
  • 1
  • 13
  • 27
  • 5
    What is the basis for that conclusion? Looking into the javadoc, it is not declared as deprecated. https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#exec-java.lang.String- – DJViking Feb 29 '16 at 07:34
  • Updating this: Since this question is used as a reference when flagging duplicates (i.e, we point the duplicates to this page), it would be nice to clarify this answer, that has more upvotes that the accepted answer. Is there a source for this information? If no, maybe it should be removed as a wring answer? – Turtle Jun 13 '17 at 07:58
  • 2
    `Runtime.getRuntime().exec()` isn't deprecated. – Daniel Jun 26 '17 at 22:09
6

You can run the command with Runtime.getRuntime().exec("<command>") (eg. Runtime.getRuntime().exec("tree")). But, this will only run executables found in path, not commands like echo, del, ... But only stuff like tree.com, netstat.com, ... To run regular commands, you will have to put cmd /c before the command (eg Runtime.getRuntime().exec("cmd /c echo echo"))

Marko Zajc
  • 307
  • 3
  • 14
3
public static void main(String[] args) {
    String command="netstat";
    try {
        Process process = Runtime.getRuntime().exec(command);
        System.out.println("the output stream is "+process.getOutputStream());
        BufferedReader reader=new BufferedReader( new InputStreamReader(process.getInputStream()));
        String s; 
        while ((s = reader.readLine()) != null){
            System.out.println("The inout stream is " + s);
        }                   
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This works.

Raja Anbazhagan
  • 4,092
  • 1
  • 44
  • 64
Anubis05
  • 1,234
  • 2
  • 13
  • 17
  • 1
    This reads every other line of the output. Should be: `String s; while ((s = reader.readLine() != null) { System.out.println(s); }` – Andrew Nguyen Apr 01 '13 at 21:39
1

Runtime#exec().

Matt Ball
  • 354,903
  • 100
  • 647
  • 710