-2

I made a program in java, and in the program i needed to use cmd,

I saw you can use Runtime().exec method, but i didn't succeed using it.
I tried to do this:

public class name{
    public static void main(String args[]){
        Runtime.exec("ipconfig");
    }
 }
ronald7894
  • 67
  • 1
  • 5
  • `exec()` is not a static method. Take a look the [API docs](http://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html). – azurefrog Jun 05 '15 at 13:41
  • possible duplicate of [How to execute system commands (linux/bsd) using Java](http://stackoverflow.com/questions/792024/how-to-execute-system-commands-linux-bsd-using-java) – azurefrog Jun 05 '15 at 13:47

1 Answers1

1

From mkyong:

    Process p = Runtime.getRuntime().exec("ifconfig");
    p.waitFor(); // waits for the application's finish

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

    String line = "";           
    while ((line = reader.readLine())!= null) {
    sb.append(line + "\n"); // read the console output 
    }
    System.out.println(sb.toString());
Sercan Ozdemir
  • 4,641
  • 3
  • 34
  • 64
  • Not necessary to wait for exec to finish. But reading from input stream will then introduce threading... – plastique Jun 05 '15 at 13:50