Reputation: 1437
Is it possible to programmatically get the address of the DNS server that's going to be used when I do my hostname resolution? I'd like to do this in a platform independent way in Java. I know there is some way to do it in Linux, some Windows APIs, etc., but can I get to this stuff from Java?
Upvotes: 11
Views: 10076
Reputation: 1250
JDK 8 example:
...
import sun.net.dns.ResolverConfiguration;
...
List nameServers = ResolverConfiguration.open().nameservers();
nameServers.forEach((dns)->System.out.println(dns));
...
Upvotes: 0
Reputation: 18675
You can use dnsjava:
import org.xbill.DNS.*;
String dnsServers[] = ResolverConfig.getCurrentConfig().servers();
(however you can't know which of the potentially multiple servers will be used for a given lookup)
Upvotes: 2
Reputation: 1853
Depending on what vendor VM you're using, you can do it by using either JNDI or a vendor-specific API (Sun/Oracle's in this case). Check this post which describes both.
I'm currently looking for a way to do the same on OpenJDK and will post details if I find anything.
EDIT: Looks like those classes are also available on the OpenJDK, interesting...
Upvotes: 1