Why is my Java app incorrectly resolving a domain name?

Issue

When attempting to resolve a domain name to an IP address, your Java application errors or resolves the wrong IP address.

Resolution

Java's name resolution behavior caches the result of a lookup forever by default. You can control this behavior with the following options:

  • networkaddress.cache.ttl (default: -1) Indicates the caching policy for successful name lookups from the name service. The value is specified as as integer to indicate the number of seconds to cache the successful lookup. A value of -1 indicates "cache forever".
  • networkaddress.cache.negative.ttl (default: 10) Indicates the caching policy for un-successful name lookups from the name service. The value is specified as as integer to indicate the number of seconds to cache the failure for un-successful lookups. A value of 0 indicates "never cache". A value of -1 indicates "cache forever".

You can set these options in your app, for example:

java.security.Security.setProperty("networkaddress.cache.ttl", "10");

For more information, see the Oracle documentation on Java Networking Properties.