InetAddress.getLocalHost() resolution on OSX Lion when offline

僤鯓⒐⒋嵵緔 提交于 2019-12-06 05:45:37

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/7892609/inetaddress-getlocalhost-resolution-on-osx-lion-when-offline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!