1

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

Keshaw Kumar
  • 317
  • 1
  • 4
  • 15

2 Answers2

1

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()

Community
  • 1
  • 1
JavaUser
  • 25,542
  • 46
  • 113
  • 139
  • I've only two adapters loopback and ethernet adapter. Also I've tied restarting my system twice. The problem still exists. – Keshaw Kumar Aug 24 '16 at 08:32
  • Can you share the code and current ipconfig output? – JavaUser Aug 24 '16 at 08:42
  • Java Code > System.out.println("IP Address : " + InetAddress.getLocalHost().getHostAddress()); Output > IP Address : 10.2.200.76 | Ipconfig output > Ethernet adapter Local Area Connection: Connection-specific DNS Suffix . : AD.AGI Link-local IPv6 Address . . . . . : fe80::b8b5:481d:ee86:9e3e%11 IPv4 Address. . . . . . . . . . . : 10.2.200.75 Subnet Mask . . . . . . . . . . . : 255.255.252.0 Default Gateway . . . . . . . . . : 10.2.203.254 – Keshaw Kumar Aug 24 '16 at 09:42
0

Somehow the host file located at %SystemRoot%\System32\drivers\etc had an entry as follows

localhost 10.2.200.76

And I think java was picking this when I used

InetAddress.getLocalHost().getHostAddress()

So, I changed the localhost entry to

localhost 127.0.0.1

Now both ipconfig and the above java api are returning the same IP address which 10.2.200.75.

Keshaw Kumar
  • 317
  • 1
  • 4
  • 15