4

在一个网络项目中。
我看到日志:

hadoop.hbase.zookeeper.ZKConfig - java.net.UnknownHostException: example.com 
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:850)
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1201)
at java.net.InetAddress.getAllByName0(InetAddress.java:1154)
at java.net.InetAddress.getAllByName(InetAddress.java:1084)
at java.net.InetAddress.getAllByName(InetAddress.java:1020)
at java.net.InetAddress.getByName(InetAddress.java:970)

但是当我ping example.com时,没关系,我也telnet example.com 2181成功!我发现了类似的问题 ,所以我在我的服务器 java DomainResolutionTest example.com
中运行 DomainResolutionTest 但没关系!

环境:

java -version

java 版本 "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, 混合模式)

os:Red Hat Enterprise Linux Server release 5.7

我很好奇为什么是Inet6AddressImpl,我想可能是Inet4AddressImpl

如何解决?
是什么原因?

4

3 回答 3

7

如果是双栈(ipv6 + v4),Java 更喜欢 ipv6。

如果您的 ipv6 以某种方式配置错误,您可以强制它更喜欢 ipv4。

设置系统属性:-Djava.net.preferIPv4Stack=true

来源: http ://docs.oracle.com/javase/6/docs/technotes/guides/net/ipv6_guide/

于 2013-08-05T11:45:31.140 回答
6

我遇到了同样的异常,并通过在 /etc/hosts 中为“localhost”条目手动设置我的主机名来解决它。

127.0.0.1       localhost DL006285-linux

# special IPv6 addresses
::1             localhost ipv6-localhost ipv6-loopback DL006285-linux
于 2015-03-05T14:33:03.510 回答
1

将子网值赋予程序可能会出现问题。在传递给程序之前,我通过修剪它来给出子网值。

subnet = subnet.trim();
int timeout = 1500;
for(int i=1;i<254;i++)
        {
        try
          {
            String host = subnet +"."+i;
            if (InetAddress.getByName(host).isReachable(timeout))
              {
                Check = Check+host+"\n";
                System.out.println(host);
              } 

           }
        catch (UnknownHostException ex) { 
            Logger.getLogger(WiFi.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(WiFi.class.getName()).log(Level.SEVERE, null, ex);
于 2015-01-25T20:51:07.460 回答