the way how we get Ip address using java code is clear and many questions about this subject were answered.
but assuming that you have Vmware or VirtualBox on your machine,so you will have extra virtual network cards each one have its own Ip Address.
when executing a little program the result was like 192.168.x.x which belongs to one of my virtual network adapters. but using "What Is My IP" the result was like 197.x.x.x
so how can i get the ip adress of the connected interface ?
this should do the trick:
import java.io.*;
import java.util.*;
public class ExecTest {
public static void main(String[] args) throws IOException {
Process result = Runtime.getRuntime().exec("traceroute -m 1 www.amazon.com");
BufferedReader output = new BufferedReader(new InputStreamReader(result.getInputStream()));
String thisLine = output.readLine();
StringTokenizer st = new StringTokenizer(thisLine);
st.nextToken();
String gateway = st.nextToken();
System.out.printf("The gateway is %s\n", gateway);
}
}
Courtesy of Chris Bunch from: How can I determine the IP of my router/gateway in Java?
Greets!