Reputation: 24104
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
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