90

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.

jhwist
  • 15,201
  • 3
  • 40
  • 47

9 Answers9

125

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.


Another answer is almost correct and I got hint from above and my problem get resolved...Thanks.

But to improve this, I am adding steps-by-steps changes, so that it will be helpful for even naive users.

Steps:

  • Open /etc/hosts, the entries might look like below.

     127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4  
     ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    
  • You need to add one more line above of this by any editor like vi or gedit (e.g. <your-machine-ip> <your-machine-name> localhost).

     192.168.1.73 my_foo localhost
    

Now, overall file may look like this:

192.168.1.73 my_foo localhost
127.0.0.1    localhost localhost.localdomain localhost4 localhost4.localdomain4
::1          localhost localhost.localdomain localhost6 localhost6.localdomain6
  • Just save it and run again your Java code... your work is done.
Nathan
  • 8,093
  • 8
  • 50
  • 76
jhwist
  • 15,201
  • 3
  • 40
  • 47
  • 1
    If you encounter permission issues when trying to write to the hosts file, instructions here will help:http://decoding.wordpress.com/2009/04/06/how-to-edit-the-hosts-file-in-mac-os-x-leopard/ – septerr Aug 06 '12 at 19:01
  • 3
    There is also a bug in OS X and java 7, details and workaround here https://groups.google.com/forum/#!topic/h2-database/DuIlTLN5KOo – Mark Lakewood Jun 16 '14 at 23:22
  • 14
    Setting `127.0.0.1 localhost ` was sufficient for me – Marius Soutier Sep 05 '14 at 11:26
  • Found this too late. Coded up a NetworkInterface.getNetworkInterfaces() solution instead. – ctpenrose Mar 24 '15 at 22:14
  • 4
    To get your hostname, you can use the `hostname` command from the terminal. – Gray Nov 05 '15 at 22:31
  • I had a similar issue under SuSE13 with Java8. Adding the host and ip to /etc/hosts helped, but afterwards nslookup was not working anymore because of a timeout. So I removed 'localhost' from the end of the line and now nslookup as well as my Java application were working correctly. – Jan Rasehorn May 04 '17 at 14:56
10

I use NetworkInterface.getNetworkInterfaces() as a fall back for when InetAddress.getLocalHost() throws an UnknownHostException. Here's the code (without exception handling for clarity).

Enumeration<NetworkInterface> iterNetwork;
Enumeration<InetAddress> iterAddress;
NetworkInterface network;
InetAddress address;

iterNetwork = NetworkInterface.getNetworkInterfaces();

while (iterNetwork.hasMoreElements())
{
   network = iterNetwork.nextElement();

   if (!network.isUp())
      continue;

   if (network.isLoopback())
      continue;

   iterAddress = network.getInetAddresses();

   while (iterAddress.hasMoreElements())
   {
      address = iterAddress.nextElement();

      if (address.isAnyLocalAddress())
         continue;

      if (address.isLoopbackAddress())
         continue;

      if (address.isMulticastAddress())
         continue;

      return address.getHostAddress();
   }
}

Other answers edit the /etc/hosts file. This is error prone, brittle, may require root access and won't work on all OS's.

Nathan
  • 8,093
  • 8
  • 50
  • 76
6

On my amazon instance I was having the same issue, there was default DNS configuration issue. So to fix the issue I had done these steps -

get your host name

$hostname
ip-10-122-16-169

ping to hostname

$ping ip-10-122-16-169
ping: unknown host ip-10-122-16-169

cat /etc/hosts file, you will get something like

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost6 localhost6.localdomain6

now you just need to append your host name at the end of the fist line, so when you append it will look like

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 ip-10-122-16-169
::1         localhost6 localhost6.localdomain6

now you're ready to go, to check ping again the same hostname

$ping ip-10-122-16-169
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=255 time=0.018 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=255 time=0.024 ms
ankit.vishen
  • 1,100
  • 15
  • 29
4

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 desvearth01 works is because the nslookup command directly consults /etc/resolv.conf. If you want to do a better command line test, use the command:

getent hosts desvearth01
Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
Martin
  • 1,073
  • 8
  • 7
4

This errors shows up when I changed the workstation name and tried start Glassfish 2. You also must rename the entry at /etc/hosts, something like this:

127.0.0.1       localhost
127.0.1.1       MyNewName
Jimmy
  • 111
  • 4
2

Checkout /etc/hostname then put your hostname to hosts file.

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
0

If you see this message than you need set hostname WITH hostname superhost.domain COMMAND!

After this, check which /etc/hosts file contain string like this 127.0.0.1 localhost.

Also, check that command uname -a returns something like this:

Linux superhost.domain 2.6.38-8-server #42-Ubuntu SMP Mon Apr 11 03:49:04 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux

NOT LIKE THIS!!!!

Linux (none) 2.6.38-8-server #42-Ubuntu SMP Mon Apr 11 03:49:04 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux

Littm
  • 4,923
  • 4
  • 30
  • 38
Pavel
  • 9
  • 1
0

Another option is in this post (in fact, what is in your /etc/sysconfig/network file for your hostname...by changing it to an FQDN name fixes this issue).

java getLocalHost() UnknownHostException /etc/hosts file differs linux api?

Community
  • 1
  • 1
Dean Hiller
  • 19,235
  • 25
  • 129
  • 212
0

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.

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
Jon
  • 1,013
  • 1
  • 10
  • 17