61

How can I check if a string is a URL in Java?

seato
  • 1,971
  • 2
  • 14
  • 18
lacas
  • 13,555
  • 29
  • 107
  • 179

8 Answers8

69

You can try to create a java.net.URL object out of it. If it is not a proper URL, a MalformedURLException will be thrown.

Joachim Sauer
  • 291,719
  • 55
  • 540
  • 600
Grodriguez
  • 21,083
  • 10
  • 57
  • 100
  • 5
    Just to be clear, it would only throw a MalformedURLException `if no protocol is specified, or an unknown protocol is found`. Unlike, Apache's UrlValidator, no additional validation is performed. – dogbane Oct 14 '10 at 10:44
  • 1
    It actually performs other checks too, e.g. port numbers. – Grodriguez Oct 14 '10 at 10:53
  • 1
    I quoted that from the javadocs http://download.oracle.com/javase/6/docs/api/java/net/URL.html. They don't state any other checks. – dogbane Oct 14 '10 at 11:05
  • 2
    That's what the Javadocs say, however the checks are performed (you can try it yourself, or have a look at the source code if you wish) – Grodriguez Oct 14 '10 at 11:12
  • Just keep in mind that this will throw a false positive if the url is numerical, such as 192.168.0.1 (without the http://). – Tommie Mar 17 '15 at 20:52
  • 1
    @Tommie "192.168.0.1" (without the http://) is not a valid URL. Creating a `java.net.URL` passing this as parameter fails with `java.net.MalformedURLException: no protocol`. This is the expected behaviour and is documented as such. No false positive here. – Grodriguez Mar 23 '15 at 08:13
  • This test fails for the URL `public static void main(String[] argv) throws MalformedURLException { URL url = new URL("http://ProductDetail/2Thumbz, Inc.?source=Admin"); }` because it **doesn't** throw an exception, despite the comma and space and period. – Chloe Jan 27 '19 at 04:56
  • The simplest solution seems to be just using URI class for validation. Also commas/dots in this example (after the auth part) should be valid, only the space is not. – Neikius Apr 15 '19 at 15:24
  • 1
    This is an example of using exceptions as flow control and it's generally considered bad, and for good reasons. Suggested reading: http://archive.fo/erYDY – Danilo Pianini Jul 08 '19 at 08:50
48

You can use UrlValidator from commons-validator. It will save you from writing code where the logic flow is guided by catching an exception, which is generally considered a bad practice. In this case, however, I think it's fine to do as others suggested, if you move this functionality to an utility method called isValidUrl(..)

yiwei
  • 3,692
  • 9
  • 33
  • 53
Bozho
  • 572,413
  • 138
  • 1,043
  • 1,132
17

For Android just add this line:

boolean isValid = URLUtil.isValidUrl( "your.uri" );
pme
  • 12,997
  • 2
  • 49
  • 80
Rafa0809
  • 1,603
  • 19
  • 23
  • This doesn't work if the scheme/prefix (e.g. "http") is missing (e.g. "stackoverflow.com") and it also doesn't work with the "ftp" scheme/prefix (e.g. "ftp://127.0.0.1"). – Neph Mar 17 '20 at 14:43
  • Hence the difference between URL and URI – Rafa0809 Mar 17 '20 at 23:03
  • 1
    I also tried the other suggestions (`new URL`/`new URI`) but `URL` doesn't like URLs without scheme (plus it feels like it only checks for the scheme, nothing else) and `URI` seems to return "true", no matter what you throw at it (even "---"). So far I've not found a way to do what op asked that works properly 99.9% of the time. – Neph Mar 18 '20 at 09:34
12

If you program in Android, you could use android.webkit.URLUtil to test.

URLUtil.isHttpUrl(url)
URLUtil.isHttpsUrl(url)

Hope it would be helpful.

pme
  • 12,997
  • 2
  • 49
  • 80
alijandro
  • 10,957
  • 2
  • 48
  • 67
  • 12
    This doesnt actually check if its a url it only checks if there is http:// in the beginning, what you need with that is URLUtil.isValidUrl(url) – Amro elaswar Nov 28 '15 at 00:34
  • 1
    Quick heads up that it's called URLUtil ... not URLUtil*S*. – Tommie Dec 06 '15 at 14:30
12

Here you go:

public static boolean isValidURL(String urlString) {
    try {
        URL url = new URL(urlString);
        url.toURI();
        return true;
    } catch (Exception e) {
        return false;
    }
}
Jared Burrows
  • 52,770
  • 23
  • 148
  • 184
BullyWiiPlaza
  • 14,779
  • 7
  • 95
  • 158
11

Complementing Bozho answer, to go more practical:

  1. Download apache commons package and uncompress it. binaries
  2. Include commons-validator-1.4.0.jar in your java build path.
  3. Test it with this sample code (reference):

    //...your imports
    
    import org.apache.commons.validator.routines.*; // Import routines package!
    
    public static void main(String[] args){
    
    // Get an UrlValidator
    UrlValidator defaultValidator = new UrlValidator(); // default schemes
    if (defaultValidator.isValid("http://www.apache.org")) {
        System.out.println("valid");
    }
    if (!defaultValidator.isValid("http//www.oops.com")) {
        System.out.println("INvalid");
    }
    
    // Get an UrlValidator with custom schemes
    String[] customSchemes = { "sftp", "scp", "https" };
    UrlValidator customValidator = new UrlValidator(customSchemes);
    if (!customValidator.isValid("http://www.apache.org")) {
        System.out.println("valid");
    }
    
    // Get an UrlValidator that allows double slashes in the path
    UrlValidator doubleSlashValidator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES);
    if (doubleSlashValidator.isValid("http://www.apache.org//projects")) {
        System.out.println("INvalid");
    }
    
  4. Run/Debug

ConcurrentHashMap
  • 4,830
  • 7
  • 42
  • 52
Esteban Cacavelos
  • 753
  • 10
  • 22
3

This function validates a URL, and returns true (valid URL) or false (invalid URL).

public static boolean isURL(String url) {
    try {
        new URL(url);
        return true;
    } catch (Exception e) {
        return false;
    }
}

Be sure to import java.net.URL;

Aaron Esau
  • 1,005
  • 3
  • 14
  • 30
  • 2
    Doesn't fail for `http://ProductDetail/2Thumbz, Inc.?source=Admin` when it should (comma, space, period). – Chloe Jan 27 '19 at 04:57
1
^(https://|http://|ftp://|ftps://)(?!-.)[^\\s/\$.?#].[^\\s]*$

This is a regex to validate URL for protocols http, https, ftp and ftps.

Paul Roub
  • 35,848
  • 27
  • 79
  • 88