imrichardcole imrichardcole - 1 year ago 246
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) {

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 Source

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download