siamii
siamii

Reputation: 24104

Find default gateway for Local Area Connection on Windows

I have tried the http://docs.oracle.com/javase/6/docs/api/java/net/NetworkInterface.html but that doesn't seem to have a field for default gateway.

The other thing I tried is execute a native ipconfig /all command and parse the result, but that varies based on the locale of the system.

Upvotes: 2

Views: 777

Answers (1)

Rajesh Pantula
Rajesh Pantula

Reputation: 10241

In place of ipconfig /all, use

netstat -rn

using Runtime.exec() and parse the results, the default gateway will the be 2nd line.

Process result = Runtime.getRuntime().exec("netstat -rn");

    BufferedReader output = new BufferedReader
        (new InputStreamReader(result.getInputStream()));

    String line = output.readLine();
    while(line != null){
        if ( line.startsWith("default") == true )
            break;              
        line = output.readLine();
    }

Upvotes: 2

Related Questions