How to verify if a String in Java is a valid URL?
-
1Possible duplicate of [Validating URL in Java](http://stackoverflow.com/questions/1600291/validating-url-in-java) – james.garriss Dec 06 '16 at 19:15
-
I also looking for this solution. Thanks a lot for this question. – Saiful Islam Sep 15 '18 at 07:31
8 Answers
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.

- 388
- 4
- 15

- 21,501
- 10
- 63
- 107
-
6Just 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
-
1I 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
-
2That'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
-
1This 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
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(..)
For Android just add this line:
boolean isValid = URLUtil.isValidUrl( "your.uri" );
-
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
-
-
1I 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
public static boolean isValidURL(String urlString) {
try {
URL url = new URL(urlString);
url.toURI();
return true;
} catch (Exception e) {
return false;
}
}
This checks according to RFC2396, like <scheme>://<authority><path>?<query>#<fragment>
, but scheme
must be known by URL
source code.
These are not valid:
telnet://melvyl.ucop.edu
news:comp.infosystems.www.servers.unix
www.google.com

- 9,501
- 5
- 69
- 106

- 17,329
- 10
- 113
- 185
If you program in Android, you could use android.webkit.URLUtil
to test.
URLUtil.isHttpUrl(url)
URLUtil.isHttpsUrl(url)
Hope it would be helpful.
-
12This 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) – Amr El Aswar Nov 28 '15 at 00:34
-
1
Complementing Bozho answer, to go more practical:
- Download apache commons package and uncompress it.
- Include commons-validator-1.4.0.jar in your java build path.
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"); }
Run/Debug

- 4,998
- 7
- 44
- 53

- 753
- 10
- 22
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;

- 1,083
- 3
- 15
- 31
-
3Doesn't fail for `http://ProductDetail/2Thumbz, Inc.?source=Admin` when it should (comma, space, period). – Chloe Jan 27 '19 at 04:57
^(https://|http://|ftp://|ftps://)(?!-.)[^\\s/\$.?#].[^\\s]*$
This is a regex to validate URL for protocols http, https, ftp and ftps.

- 36,322
- 27
- 84
- 93

- 19
- 3