Getting DNS server address programmatically in Java

Can I programmatically get the address of the DNS server that will be used when I resolve the host name? I would like to make this a platform independent way in Java. I know there is some way to do this on Linux, some Windows APIs, etc., but can I get this stuff from Java?

+6
source share
3 answers

Depending on which vendor virtual machine you are using, you can do this using JNDI or a vendor-specific API (in this case, Sun / Oracle). Check out this post that describes both.

I'm currently looking for a way to do the same on OpenJDK and post details if I find anything.

EDIT: It seems that classes are also available on OpenJDK, interesting ...

+1
source

You can use dnsjava :

import org.xbill.DNS.*;

String dnsServers[] = ResolverConfig.getCurrentConfig().servers();

(however, you cannot know which of several servers will be used for this search)

+1
source

JDK 8 example:

...
import sun.net.dns.ResolverConfiguration;
...
List nameServers = ResolverConfiguration.open().nameservers();
nameServers.forEach((dns)->System.out.println(dns));
...
0
source

Source: https://habr.com/ru/post/1528014/


All Articles