How to use Java to check whether a domain exists without any external libraries

You wanted to buy a domain for your personal website. You found a whois service and it told you that your domain is available. You are elated and tried to acquire the domain through your favourite registrar. However, your registrar told you that the domain had already been taken.

Isn't that frustrating? Well, it happened to me once and I was determined never to use the whois service of those unscrupulous sites ever again.

So how can you check whether a domain name is available by yourself? When you have a computer at hand, you can find several ways to do so. In this post, I share how you can check whether a domain exists with the Java programming without using any external libraries.

Example Java codes to check whether a domain exists

When you want to check whether a domain exists, you can use the getByName method of the InetAddress class to check whether a domain name had been taken or not:

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

public class CheckIfDomainNameIsTaken {

    public static void main(String[] args) {

        String domainToCheck = "google.com";

        if (args.length > 1) {
            // Get domainToCheck from command line arguments if there is one being supplied
            domainToCheck = args[0];
        }

        InetAddress domainInetAddress = null;

        try {
            domainInetAddress = InetAddress.getByName(domainToCheck);

            System.out.println("Somebody had taken the domain: " + domainToCheck);
            System.out.println("Domain information: ");
            System.out.println(domainInetAddress);
        } catch (UnknownHostException uhe) {
            System.out.println("Congratulations! No host information can be found for domain: " + domainToCheck);
            System.out.println("There may be a possibility that " + domainToCheck + " is still available for registration. ");
        }

    }

}

As shown above, we first import the java.net.InetAddress and java.net.UnknownHostException classes before we declare our Java class. After that, we get started with building our Java application by declaring a class with a main method.

Within the main method, we first declare a default domainToCheck to contain the domain name that we wish to check for existence. After that, we get the first command line argument as the domainToCheck if command line arguments are supplied to our Java program.

Once we had a domain value to run the domain availability check, we attempt to get an InetAddress object and set it to domainInetAddress. When a domain information cannot be found, InetAddress.getByName(domainToCheck) will throw a UnknownHostException exception.

After an UnknownHostException is thrown, we print some message to denote that there is a possibility that the domain is still available for registration. On the other hand, we print some message along with some information of the domain to denote that the domain was taken.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.