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?
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.
In Java, you can use InetAddress. getLocalHost() to get the Ip Address of the current Server running the Java app.
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With