Fixing UnknownHostException bug for jmdns on linux

by Martin Monperrus
Here is a description of a bug I encountered while working with jmdns.

Problem

Aug 26, 2011 12:16:52 AM javax.jmdns.impl.HostInfo newHostInfo
WARNING: Could not intialize the host network interface on nullbecause of an error: beethoven: beethoven
java.net.UnknownHostException: beethoven: beethoven
	at java.net.InetAddress.getLocalHost(InetAddress.java:1426)
	at javax.jmdns.impl.HostInfo.newHostInfo(HostInfo.java:75)
	at javax.jmdns.impl.JmDNSImpl.(JmDNSImpl.java:428)
	at javax.jmdns.JmDNS.create(JmDNS.java:41)
	at net.ioncannon.ap4j.main.Main.startMDNS(Unknown Source)
	at net.ioncannon.ap4j.main.Main.main(Unknown Source)

Cause

The host name defined in /etc/hostname is not resolvable.
$ cat /etc/hostname 
beethoven
$ gethostip beethoven
beethoven: Unknown host

Solution

Edit /etc/hosts to resolve the host name
127.0.0.1	localhost localhost.localdomain beethoven
That's fine:
$ gethostip beethoven
localhost 127.0.0.1 7F000001
Tagged as: