Thursday, August 16, 2012

Oracle Kills getLocalhost on MacOS X in Java 7

This is going to be another one of those How things that can't happen actually happen post.
I recently updated my Mac to Java 7. Pretty late though. Right after that DistributeMe services start behaving at least strange. Right after the start a service registers itself in a registry with a unique service identifier, which looks like that:


Where being the ip adress of the machine the service runs on. This adress is used by the client, once the later wants to connect to the service.

After upgrade to Java 7 the service registered itself in the registry with the identifier:


Of course no client were able to resolve the host named unknown. I started investigating where this comes from, and well, it was in one of those sections...

private static String getHostName(){
    InetAddress localhost = InetAddress.getLocalHost();
    String host = localhost.getHostAddress();
    HashMap mappings = configuration.getMappings(); 
    String mappedHost = mappings.get(host);
    return mappedHost == null ? host : mappedHost;
  }catch(UnknownHostException e){
    return "unknown";

Ok, this is certainly not the best idea to return "unknown" here (one of those, can't happen anyway bug), but why the heck did InetAddress.getLocalHost() fail?

I wrote a very small program to verify it:

package localhostbug;


public class PrintLocalhost {
  public static void main(String[] args) throws Exception{
    InetAddress localhost = InetAddress.getLocalHost();
    String host = localhost.getHostAddress();
    System.out.println("host: "+host);

Running it with JAVA6 and JAVA7 shows the difference, watch yourself:

$JAVA6_HOME/bin/java -cp classes localhostbug.PrintLocalhost
$JAVA7_HOME/bin/java -cp classes localhostbug.PrintLocalhost
Exception in thread "main" colin.speedport.ip: colin.speedport.ip: nodename nor servname provided, or not known
 at localhostbug.PrintLocalhost.main(
Caused by: colin.speedport.ip: nodename nor servname provided, or not known
 at Method)
 ... 1 more

After some googling I found a bug in oracle bug database and a similar issue in openjdk. Seems Java and Mac is getting less and less a love story.


  1. As a workaround, add the following parameter

  2. adding my local host name to /etc/hosts worked, whereas the VM option did not: localhost deep-thought

  3. Leon, InetAddress.getLocalHost() is designed to give you the host's own address. But, really, what is a host's own address? This is - and has always been - a question that often have an ambiguous answer if you are on a multi-homed host. True that many people use the method and it works well for them in their little sandbox environment. But then you move your application to some bigger host that has 4 NICs and thus can be identified (accessed) by 4 different IP addresses ... then what?

    So I would argue that relying on InetAddress.getLocalHost() in the first place is simply wrong.

    1. From the practical point of view it used to return the first address in /etc/hosts. For a multi-homed host you usually can pick one address as bind address, but using the first as default usually worked. Now it doesn't ;-)