0

After I registered dnsjava as default Java DNS provider I get a problem. It can't resolve local addresses which described in /etc/hosts file on my Linux machine. This file look something like this:

127.0.0.1   localhost
127.0.1.1   servername

So if I try to resolve one of such names UnknownHostException happens:

org.xbill.DNS.Address.getByName("localhost");
org.xbill.DNS.Address.getByName("servername");

It's not a problem when you're using dnsjava along with default dns provider. Being a sole provider, dnsjava causes lots of errors in default libraries, which turn out to be highly dependent on localhost resolution capability. So, the question is: how to change behavior of dnsjava to resolve local hostnames?

Edit. Next code works fine:

java.net.InetAddress.getByName("localhost");

But java.net.InetAddress.getLocalHost() method throws:

java.net.UnknownHostException: servername

1 Answer 1

1

dnsjava is a DNS client library; it talks to DNS servers. /etc/hosts is not part of the DNS protocol nor does dnsjava know anything about it.

See this old post on a the dnsjava users mailing list from the guy who wrote it: http://old.nabble.com/DNS-Resolve-from-hosts-file-first-then-DNS-Server-td15431381.html

Nothing has changed in that regard.

If java.net.InetAddress.getByName() is working, then your DNS server is configured to respond to queries for localhost.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the link! All of it sound normally. But method java.net.InetAddress.getLocalHost() attempts to return servername as local host name in my case. Because of DNS server doesn't know about such hostname I get UHE. Problem is that too many libraries want to know local host name. I can't change behavior of libraries and DNS server so I should change it in dnsjava.
NOTE: The URL in this answer no longer works (thanks Nabble), but it's available on the Wayback machine at web.archive.org/web/20111220074414/http://old.nabble.com/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.