InetAddress.getLocalHost() fails intermittently

Viewed 32

I am debugging an application which is failing to bootup with the UnknownHostException exception while it's trying to fetch the hostname. So to reproduce this I setup the machine and manually turned off all the DNS resolutions and ran the same application.

  • Remove the hostname and localhost entries from /etc/hosts.
  • Remove the host to IP mapping from my DNS server.

I was able to successfully reproduce and it fails with the same exception.

InetAddress local;
            try {
                local = InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
                throw new MalformedURLException("Local host name unknown: " +
                                                e);
            }

However in the same machine I wrote a simple Java code to do the same resolution but I don't see the issue when I run my script.

Here is what I am running

import java.net.InetAddress;
import java.net.UnknownHostException;

public class TestDNSResolution {

  public static void main(String[] args) throws UnknownHostException {
    try {
      InetAddress inetAddress;
      inetAddress = InetAddress.getLocalHost();
      System.out.printf("inetAddress: %s", inetAddress);
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
  }
}

Output:

$ java TestDNSResolution
inetAddress: ****<actualhostname>****/10.1.6.109

I was actually expecting this to throw an exception.

Any pointers on what I am missing or anyways I can make this fail from the Java code?

0 Answers
Related