A strange UnknownHostException

孤街醉人 提交于 2019-12-10 15:39:34

问题


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

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

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)

os:Red Hat Enterprise Linux Server release 5.7

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

How to solve it?
What's the cause ?


回答1:


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

127.0.0.1       localhost DL006285-linux

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



回答2:


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

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

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

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




回答3:


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


来源:https://stackoverflow.com/questions/18056979/a-strange-unknownhostexception

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