imrichardcole imrichardcole - 25 days ago 24
Java Question

InetAddress.getLocalHost() slow to run (30+ seconds)

With the following code:

try {
System.out.println(new Date());
InetAddress hostName = InetAddress.getLocalHost();
System.out.println(new Date());
} catch (UnknownHostException e) {
e.printStackTrace();
}


I get this output:

Thu Oct 22 20:58:22 BST 2015
Thu Oct 22 20:58:52 BST 2015


In other words 30 seconds to execute. Machine is 2015 Macbook Pro with Java 1.8.0_60.

Why does this take so long?

Answer

The issue can be solved by adding the following to /etc/hosts (assuming hostname is macbook:

127.0.0.1   macbook
::1         macbook

This returns the time to something more suitable (< 1 second)

Comments