The UnknownHostException and A Not-so-obvious Cause

June 9, 2008 – 20:12 | java | Tags: , , ,

The past week has turned out to be the week of “all sorts of weird problems” for me. One of the issues I ran into was that, in a new Ubuntu installation, maven fails to download artifacts from the repository server. After some arm-twisting with maven (people really should start logging the actually exception instead of just printing “unable to download artifact…”), I was able to pinpoint the underlying problem to be an UnknownHostException from trying to resolve the repository server. The strangeness of the situation is, I can access the repo and download the same artifact with firefox, wget, or anything non-java. I can also resolve the repo server name just fine with nslookup.

And here comes the second layer of strangeness - I also have an ubuntu virtualbox machine running in the same box, with the same JDK version, same setup, and the same source tree. And it works just fine in there.

So fast-forward through a few hours of hair pulling, the two workarounds I eventually figured out are:

  • The /etc/nsswitch.conf file out-of-box has a line looking like this:
    hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4
    Apparently the name resolution code in JVM (e.g. InetAddress.getByName() and friends) doesn’t like the multicasting DNS (represented by ‘mdns4_minimal’).
    Move ‘dns’ up so it looks like:
    hosts: files dns mdns4_minimal [NOTFOUND=return] mdns4
    solves the problem. On the other hand, I haven’t observed any negative effect of this change on any of the non-Java applications.
  • If you are not comfortable with or don’t have control over changing a box-wide config file, another workaround is to set the system property java.net.preferIPv4Stack to true when starting a Java application (maven in my case) which needs to do name resolution.

So how come the default works fine in the virtual machine? My theory is that when running in there, the virtualbox NAT code running in the guest OS is called for name resolution during ‘files’-based resolution, and is able to handle it by delegating to the host OS.

Trackback from your site, or follow the comments in RSS.
  1. 1 Trackback(s)

  2. Oct 9, 2008: UnknownHostException: Java & IPV6 on Linux - revetkn.com

Post a Comment