脱机时OSX Lion上的InetAddress.getLocalHost()分辨率 [英] InetAddress.getLocalHost() resolution on OSX Lion when offline

查看:70
本文介绍了脱机时OSX Lion上的InetAddress.getLocalHost()分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在脱机工作(即未连接到Internet)时,是否有人在OSX Lion的Java中遇到Java的InetAddress.getLocalHost()分辨率问题?

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

似乎根本无法解析localhost:127.0.0.1:

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)

我的/etc/hosts中没有什么特别的

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

我怀疑这不是Java问题,而是OSX Lion DNS解析问题.

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?

关于如何在OSX级别上进一步调试的任何提示?

Any tips on how debug this further at OSX level?

UPDATE 26/10/2011-这可能是JDK错误,下面是测试:

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

干杯, 加尔德(Galder)

Cheers, Galder

推荐答案

找到了解决此问题的方法,只需将localhost的别名添加到网络接口:

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.

这篇关于脱机时OSX Lion上的InetAddress.getLocalHost()分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆