Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A strange UnknownHostException

Tags:

java

ipv6

dns

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 ?

like image 349
fuyou001 Avatar asked Dec 09 '22 13:12

fuyou001


2 Answers

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/

like image 104
LarsK Avatar answered Dec 31 '22 06:12

LarsK


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
like image 38
Hans Wurst Avatar answered Dec 31 '22 04:12

Hans Wurst