Validating URL in Java Validating URL in Java java java

Validating URL in Java


For the benefit of the community, since this thread is top on Google when searching for
"url validator java"


Catching exceptions is expensive, and should be avoided when possible. If you just want to verify your String is a valid URL, you can use the UrlValidator class from the Apache Commons Validator project.

For example:

String[] schemes = {"http","https"}; // DEFAULT schemes = "http", "https", "ftp"UrlValidator urlValidator = new UrlValidator(schemes);if (urlValidator.isValid("ftp://foo.bar.com/")) {   System.out.println("URL is valid");} else {   System.out.println("URL is invalid");}


The java.net.URL class is in fact not at all a good way of validating URLs. MalformedURLException is not thrown on all malformed URLs during construction. Catching IOException on java.net.URL#openConnection().connect() does not validate URL either, only tell wether or not the connection can be established.

Consider this piece of code:

    try {        new URL("http://.com");        new URL("http://com.");        new URL("http:// ");        new URL("ftp://::::@example.com");    } catch (MalformedURLException malformedURLException) {        malformedURLException.printStackTrace();    }

..which does not throw any exceptions.

I recommend using some validation API implemented using a context free grammar, or in very simplified validation just use regular expressions. However I need someone to suggest a superior or standard API for this, I only recently started searching for it myself.

NoteIt has been suggested that URL#toURI() in combination with handling of the exception java.net. URISyntaxException can facilitate validation of URLs. However, this method only catches one of the very simple cases above.

The conclusion is that there is no standard java URL parser to validate URLs.


You need to create both a URL object and a URLConnection object. The following code will test both the format of the URL and whether a connection can be established:

try {    URL url = new URL("http://www.yoursite.com/");    URLConnection conn = url.openConnection();    conn.connect();} catch (MalformedURLException e) {    // the URL is not in a valid form} catch (IOException e) {    // the connection couldn't be established}