Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the address of a DNS server programmatically in Java

Tags:

java

dns

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?

like image 844
Sander Smith Avatar asked Feb 21 '14 01:02

Sander Smith


People also ask

How do I find DNS server address?

Open your Command Prompt from the Start menu (or type “Cmd” into the search in your Windows task bar). Next, type ipconfig/all into your command prompt and press Enter. Look for the field labeled “DNS Servers.” The first address is the primary DNS server, and the next address is the secondary DNS server.

How do I find my server IP in Java?

In Java, you can use InetAddress. getLocalHost() to get the Ip Address of the current Server running the Java app.

How do I query a DNS server for an IP address?

Open the “Command Prompt” and type “ipconfig /all”. Find the IP address of the DNS and ping it. If you were able to reach the DNS server through a ping, then that means that the server is alive. Try performing simple nslookup commands.

What is Dnsjava?

dnsjava is an implementation of DNS in Java. It. supports almost all defined record types (including the DNSSEC types), and unknown types. can be used for queries, zone transfers, and dynamic updates. includes a cache which can be used by clients, and an authoritative only server.


1 Answers

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...

like image 199
Roberto Andrade Avatar answered Sep 21 '22 16:09

Roberto Andrade