Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose DNS server for resolving hostnames in Java

Tags:

java

dns

resolve

resolving a hostname to an IP address is rather easy in Java by using the InetAddress class like this:

InetAddress address = InetAddress.getByName("www.example.com");

But this method uses the DNS server which is used by the running system.

Is there any way to specify the DNS server that should be used for resolving?

like image 673
coroner Avatar asked Jul 03 '13 09:07

coroner


People also ask

How does Windows decide which DNS server to use when resolving names?

It doesn't decide randomly. You're connected to a router which gets it IP from a company which has DNS servers. They get your request unless you alter the IP manually to another DNS, for instance: OpenDns. Or perhaps you decide on having your own DNS servers.

How do I force nslookup to use a specific DNS server?

Type nslookup and hit Enter. The displayed information will be your local DNS server and its IP address. You can specify the DNS server (IP address), type of record, and domain name. Type nslookup and domain name and the command will return the A record for the domain you run a query for.

What DNS resolving?

DNS (Domain Name Server) resolution is the process of translating IP addresses to domain names. When a profile is configured to look up all numeric IP addresses, Webtrends makes a call to the network's DNS server to resolve DNS entries.


1 Answers

If you use Sun Java, you can use this code:

//Override system DNS setting with Google free DNS server
System.setProperty("sun.net.spi.nameservice.nameservers", "8.8.8.8");
System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");

See this blog post: How to set a custom DNS server with Java System properties for more details.

like image 180
Loša Avatar answered Oct 15 '22 13:10

Loša