views:

330

answers:

3

Hi,
I am testing our server-application (written Java) on different operating systems and thought that OpenSolaris (2008.11) would be the least troublesome due to the nice Java integration. Turns out I was wrong, as I end up with a UnknownHostException

try {
  computerName = InetAddress.getLocalHost().getHostName();
  if (computerName.indexOf(".") > -1)
    computerName = computerName.substring(0,
        computerName.indexOf(".")).toUpperCase();
} catch (UnknownHostException e) {
  e.printStackTrace();
}

The output is:

java.net.UnknownHostException: desvearth01: desvearth01
    at java.net.InetAddress.getLocalHost(InetAddress.java:1353)

However, nslookup desvearth01 returns the correct IP address, and nslookup localhost returns 127.0.0.1 as expected. Also, the same code works perfectly on FreeBSD. Is there anything special to OpenSolaris that I am not aware of?

Any hints appreciated, thanks.

+2  A: 

In good tradition I can answer my own question once again:

It seems that InetAddress.getLocalHost() ignores the /etc/resolv.conf but only looks at the /etc/hosts file (where I hadn't specified anything besides localhost). Adding the IP and hostname to this file solves the problem and the exception is gone.

jhwist
A: 

Host lookups on Solaris uses /etc/nsswitch.conf so depending on what the 'hosts:' line says it determines if /etc/hosts, NIS, DNS and/or LDAP should be consulted.

If you only use hosts and DNS you should have this in /etc/nsswitch.conf:

hosts: files dns

The reason 'nslookup desvearth01works is because thenslookupcommand directly consults/etc/resolv.conf`. If you want to do a better command line test, use the command:

getent hosts desvearth01
Martin
A: 

I am having issues around this as well. I need to do further testing, but it looks like NetworkInterface.getNetworkInterfaces() can be more reliable. I think that this does not do the lookup, but just grabs the IP.

I am using it as the 'next best' when the getLocalHost() fails.

Jon