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:

rmi://a_b_c_FooService.qhxgttedwr@192.168.1.113:9252@20120816162707

Where 192.168.1.113 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:

rmi://a_b_c_FooService.qhxgttedwr@unknown:9252@20120816162707

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...

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.

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 ;-)