Perform NSLookup with the InetAddress class


This code example performs a NS lookup and prints out the IP-address along with the host name.
This is possible due to the class java.net.InetAdress which we can instantiate through the static method getByName().


import java.net.InetAddress;
import java.net.UnknownHostException;

/*
 * Main.java
 *
 * @author www.javadb.com
 */

public class Main {
    
    /*
     * This method performs a NS Lookup
     */

    public void performNSLookup() {
        
        try {
            
            InetAddress inetHost = InetAddress.getByName("cnn.com");
            String hostName = inetHost.getHostName();
            System.out.println("The host name was: " + hostName);
            System.out.println("The hosts IP address is: " + inetHost.getHostAddress());
            
        } catch(UnknownHostException ex) {
            
            System.out.println("Unrecognized host");
        }
    }
    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        new Main().performNSLookup();
    }
}