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
Community
  • 1
  • 1
artspb
  • 1,127
  • 1
  • 10
  • 19

1 Answers1

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.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • 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. – artspb May 28 '13 at 06:00
  • 1
    NOTE: The URL in this answer no longer works (thanks Nabble), but it's available on the Wayback machine at https://web.archive.org/web/20111220074414/http://old.nabble.com/DNS-Resolve-from-hosts-file-first-then-DNS-Server-td15431381.html – Drizzt321 May 11 '15 at 19:10