A Java program to print a DNS (Domain Name System) record for an Internet Address



// Print out DNS Record for an Internet Address
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class DNSLookup {
    public static void main(String args[]) {
        // explain what program does and how to use it
        if (args.length != 1) {
            System.err.println("Print out DNS Record for an Internet Address");
            System.err.println("USAGE: java DNSLookup domainName|domainAddress");
        try {
            InetAddress inetAddress;
            // if first character is a digit then assume is an address
            if (Character.isDigit(args[0].charAt(0))) {
                // convert address from string representation to byte array
                byte[] b = new byte[4];
                String[] bytes = args[0].split("[.]");
                for (int i = 0; i < bytes.length; i++) {
                    b[i] = Integer.valueOf(bytes[i]).byteValue();
                // get Internet Address of this host address
                inetAddress = InetAddress.getByAddress(b);
            else {
                // get Internet Address of this host name
                inetAddress = InetAddress.getByName(args[0]);
            // show the Internet Address as name/address
            System.out.println(inetAddress.getHostName() + "/" + inetAddress.getHostAddress());
            // get the default initial Directory Context
            InitialDirContext iDirC = new InitialDirContext();
            // get all the DNS records for inetAddress
            Attributes attributes =
                iDirC.getAttributes("dns:/" + inetAddress.getHostName(), new String[] {"*"});
            // get an enumeration of the attributes and print them out
            NamingEnumeration<? extends Attribute> attributeEnumeration = attributes.getAll();
            System.out.println("-- DNS INFORMATION --");
            while (attributeEnumeration.hasMore()) {
                System.out.println("" + attributeEnumeration.next());
        catch (UnknownHostException exception) {
            System.err.println("ERROR: No Internet Address for '" + args[0] + "'");
        catch (NamingException exception) {
            System.err.println("ERROR: No DNS record for '" + args[0] + "'");

Sample Runs

c:\Users\admin\Documents\JAVA>java -version
java -version
java version "13.0.1" 2019-10-15
Java(TM) SE Runtime Environment (build 13.0.1+9)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)

c:\Users\admin\Documents\JAVA>javac -Xlint DNSLookup.java
javac -Xlint DNSLookup.java

c:\Users\admin\Documents\JAVA>java DNSLookup.java
java DNSLookup.java
Print out DNS Record for an Internet Address
USAGE: java DNSLookup domainName|domainAddress

c:\Users\admin\Documents\JAVA>java DNSLookup yahoo.com
java DNSLookup yahoo.com
AAAA: 2001:4998:58:1836::11, 2001:4998:58:1836::10, 2001:4998:44:41d::3, 2001:4998:44:41d::4, 2001:4998:c:1023::5, 2001:4998:c:1023::4
NS: ns1.yahoo.com., ns5.yahoo.com., ns4.yahoo.com., ns3.yahoo.com., ns2.yahoo.com.

c:\Users\admin\Documents\JAVA>java DNSLookup
java DNSLookup
AAAA: 2001:4998:58:1836::10

c:\Users\admin\Documents\JAVA>java DNSLookup 199.232.41
java DNSLookup 199.232.41
ERROR: No DNS record for '199.232.41'

c:\Users\admin\Documents\JAVA>java DNSLookup ghthytf.com
java DNSLookup ghthytf.com
ERROR: No Internet Address for 'ghthytf.com'

Copyright © 2019 Shayne Steele  
Comments / Suggestions / Corrections to : (shayne.steele <AT> eyeasme <DOT> com)
This document is licensed under the GFDLv1.3, All software shown is licensed under the LGPLv3.
Document last modified on December 11, 2019 07:22 pm EST.