Search code examples
javaipipconfig

Java program and ipconfig shows different IP address


Recently the IP address of my system as show by the ipconfig has changed from 10.2.200.76 to 10.2.200.75 but the IP address returned by the following java code

InetAddress.getLocalHost().getHostAddress()

is still 10.2.200.76. My question is, "how is that possible?" I'm speculating that something is wrong with the either DHCP or Java.


System Details - OS : Windows 7, Java : Oracle JDK 7, Ethernet Card : Intel(R) 82579LM Gigabit

Update 1: Java Code

public static void main(String[] args) {
    try {
        System.out.println("IP Address : " + InetAddress.getLocalHost().getHostAddress());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Output of java code> IP Address : 10.2.200.76

Ipconfig Output: Output of ipconfig


Solution

  • Can you enumerate all the IP addresses and see your old IP is still there in outout. Also try to restart the machine and check it again.Refer below.

    InetAddress.getLocalHost().getHostAddress() is returning 127.0.1.1

    http://docs.oracle.com/javase/6/docs/api/java/net/NetworkInterface.html#getInetAddresses()