Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InetAddress.getLocalHost() throws UnknownHostException

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.

like image 967
jhwist Avatar asked Dec 10 '09 15:12

jhwist


People also ask

What is InetAddress getLocalHost ()?

Java InetAddress getLocalHost() method The getLocalHost() method of Java InetAddress class returns the instance of InetAddress containing local host name and address. In this, firstly the host name is retrieved from the system, then that name is resolved into InetAddress.

How do you resolve an unknown host exception in Java?

To resolve application level issues, try the following methods: Restart your application. Confirm that your Java application doesn't have a bad DNS cache. If possible, configure your application to adhere to the DNS TTL.

What does InetAddress getByName do?

getByName. Determines the IP address of a host, given the host's name. The host name can either be a machine name, such as " java.sun.com ", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.


1 Answers

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.
like image 110
jhwist Avatar answered Sep 20 '22 11:09

jhwist