4

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

3
  • What does ping localhost in Terminal.app show?
    – trojanfoe
    Oct 25, 2011 at 17:22
  • So you are just calling InetAddress.getLocalHost(); and you get that exception? Wow. This is on Java1.6 on Mac OSX 10.7.X? I seems to work for me.
    – Gray
    Oct 25, 2011 at 19:18
  • Mac OSX 10.7.2 with JDK: java version "1.6.0_26" Java(TM) SE Runtime Environment (build 1.6.0_26-b03-383-11A511) Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-383, mixed mode) Oct 26, 2011 at 9:16

1 Answer 1

8

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.