How to resolve java.net.UnknownHostException when connecting to localhost by hostname

Many a times while working with Java applications, in my case scala (spark), which use your hostname (not localhost) but the actual name you gave to your system, like I use rohit.tuplejump.com, will fail to connect throwing an UnknownHostException.

If you do a hostname -f, it will say "Name or service not known"

Many people use the solution of adding the this entry to /etc/hosts but that needs to be changed every time your public IP changes. The better solution is to use the nss-myhostname package.

The package is generally installed on any linux distribution, in case it isn't you can search and install it using your package manager. On Fedora you can do,
$ sudo yum install nss-myhostname
Then edit your /etc/nsswitch.conf to add myhostname in the line which begins with "hosts:". This will tell the system to use bind your system hostname to your public IP address. On my system this line looked like,
hosts:      files mdns4_minimal dns
and I changed it to,
hosts:      files mdns4_minimal dns myhostname
Now, "hostname -f" will work and so will your application.

#
​IBM #websphere​






Popular posts from this blog

Shrink you container size up to 95%.

alma linux: dnf Module yaml error: Unexpected key in data