Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you check whether domain exists or not in Java?

Tags:

java

Suppose my email address is [email protected] and I want to check if yahoo.com is a valid domain or not.

Can anyone tell me which Java API I can use for this?

like image 236
Nutan Sharma Avatar asked May 09 '11 06:05

Nutan Sharma


2 Answers

You can use org.apache.commons.validator.routines.DomainValidator.

First add maven dependency

<dependency>
        <groupId>commons-validator</groupId>
        <artifactId>commons-validator</artifactId>
        <version>1.6</version>
</dependency>

then:

boolean isValid = DomainValidator.getInstance().isValid("yahoo.com");
like image 82
songxin Avatar answered Oct 06 '22 08:10

songxin


One thing that you could do is trying to resolve "yahoo.com". Something like this:

public static void main(String[] args) throws UnknownHostException {
    InetAddress inetAddress = InetAddress.getByName("yahoo.com");
    System.out.println(inetAddress.getHostName());
    System.out.println(inetAddress.getHostAddress());
}

which outputs:

yahoo.com
67.195.160.76
like image 36
MarcoS Avatar answered Oct 06 '22 08:10

MarcoS