How do I obtain the real-time multiple IP addresses for a given machine?

Tim Rohaly

As you discovered, InetAddress keeps a cache of names that have been looked up. This is the practice recommended by RFC 1034 (Domain Names - Concepts and Facilities) for resolvers, although InetAddress doesn't pay attention to the "refresh" information returned by the name server. The refresh interval indicates how long a particular name may be assumed valid; the cache should expire the name after this interval, but doesn't.

The cache is maintained as a private HashMap, so you can't override it. The cache is not part of the public definition of InetAddress, so the caching behavior is not something that is guaranteed across implementations.

If you are using Sun's implementation of Java, you may set the sun.net.inetaddr.ttl property to the number of seconds that each name is cached. The default value is -1, meaning names are cached forever. Remember, this is specific to Sun's implementation.

There's no other way around this problem, other than reloading the InetAddress class, because the cache is maintained as a class variable. To reload InetAddress, you will have to create a new class loader each time you need to see changes, then instantiate InetAddress each time using the new class loader.