Java 一个奇怪的 UnknownHostException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18056979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:06:33  来源:igfitidea点击:

A strange UnknownHostException

javadnsipv6

提问by fuyou001

In a web project .
I see the log:

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

hadoop.hbase.zookeeper.ZKConfig - java.net.UnknownHostException: example.com 
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress.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)

but when I ping example.com, it's ok, I also telnet example.com 2181successfully! I found the similar questionso I run the DomainResolutionTest in my Server java DomainResolutionTest example.com
but it's ok !

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

env:

环境:

java -version

java version "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, mixed mode)

java version "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

os:Red Hat Enterprise Linux Server release 5.7

I am curious why is Inet6AddressImpl,I think it may be Inet4AddressImpl

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

How to solve it?
What's the cause ?

如何解决?
是什么原因?

采纳答案by Hans Wurst

i have had the same exception and solved it by setting my hostname manually in /etc/hosts for the 'localhost' entry both.

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

127.0.0.1       localhost DL006285-linux

# special IPv6 addresses
::1             localhost ipv6-localhost ipv6-loopback DL006285-linux

回答by LarsK

If it is a dual stack (ipv6 + v4) Java prefers ipv6.

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

You can force it to prefer ipv4, if your ipv6 is misconfigured somehow.

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

Set system property with: -Djava.net.preferIPv4Stack=true

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

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

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

回答by Laxminarayan

There may be problem in giving the subnet value to the program. I gave the subnet value by trimming it before passing to the program.

为程序提供子网值可能有问题。在传递给程序之前,我通过修剪它来给出子网值。

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);