Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InetAddress.getLocalHost() resolution on OSX Lion when offline

Is anyone having issues with Java's InetAddress.getLocalHost() resolution in Java with OSX Lion when working offline (i.e. not connected to internet)?

It would appear that localhost:127.0.0.1 is not resolved at all:

Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    at java.net.InetAddress.getLocalHost(InetAddress.java:1356)

Nothing special in my /etc/hosts:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost 
fe80::1%lo0     localhost

I suspect this is not a Java issue, but rather an OSX Lion DNS resolution issue.

Can anyone help?

If this is not the right forum, where can I ask about this?

Any tips on how debug this further at OSX level?

UPDATE 26/10/2011 - This could be JDK bug, the following test:

InetAddress addr;
try {
   addr = InetAddress.getLocalHost();
   System.out.println("With localhost access: " + addr);
} catch (ArrayIndexOutOfBoundsException e) {
   addr = InetAddress.getByName(null);
   System.out.println("With reverse lookup: " + addr);
}

Would print the following when offline:

With reverse lookup: localhost/127.0.0.1

Cheers, Galder

like image 599
Galder Zamarreño Avatar asked Feb 23 '23 14:02

Galder Zamarreño


1 Answers

Found a way to workaround this issue, just add an alias for localhost to the network interface:

sudo ifconfig en0 alias 127.0.0.1

Once that's in place, I get no localhost issues any more when offline.

like image 166
Galder Zamarreño Avatar answered Mar 03 '23 15:03

Galder Zamarreño