Check if URL is valid or not in Java Last Updated : 30 May, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a URL as string, we need to find if the given URL is valid or not. Input : str = "https://www.geeksforgeeks.org/" Output : Yes Input : str = "https:// www.geeksforgeeks.org/" Output : No Note that there is a space after https:// Using java.net.url We can use java.net.url class to validate a URL. The idea is to create a URL object from the specified String representation. If we do not get exception while creating the object, we return true. Else we return false. Java // Java program to check if a URL is valid // using java.net.url import java.net.URL; class Test { /* Returns true if url is valid */ public static boolean isValid(String url) { /* Try creating a valid URL */ try { new URL(url).toURI(); return true; } // If there was an Exception // while creating URL object catch (Exception e) { return false; } } /*driver function*/ public static void main(String[] args) { String url1 = "https://www.geeksforgeeks.org/"; if (isValid(url1)) System.out.println("Yes"); else System.out.println("No"); String url2 = "http:// www.geeksforgeeks.org/"; if (isValid(url2)) System.out.println("Yes"); else System.out.println("No"); } } Output: Yes No Comment More infoAdvertise with us Next Article Finding IP address of a URL in Java P pranav gupta Improve Article Tags : Misc Java java-regular-expression Practice Tags : JavaMisc Similar Reads Check if an URL is valid or not using Regular Expression Given a URL as a character string str of size N.The task is to check if the given URL is valid or not.Examples : Input : str = "https://www.geeksforgeeks.org/" Output : Yes Explanation : The above URL is a valid URL.Input : str = "https:// www.geeksforgeeks.org/" Output : No Explanation : Note that 4 min read java.net.URL Class in Java URL is an acronym of Uniform resource locator. It is a pointer to locate resource in www (World Wide Web). A resource can be anything from a simple text file to any other like images, file directory etc. The typical URL may look like http://www.example.com:80/index.htmlThe URL has the following part 4 min read Finding IP address of a URL in Java Prerequisite: InetAddressgetByName() : Returns the InetAddress of the given host. If the host is a literal IP address, then only its validity is checked. Fetches public IP Address of the host specified. It takes the host as an argument and returns the corresponding IP address. Examples:Â Input : www 2 min read URL toURI() method in Java with Examples The getURI() function of URL class converts the URL object to a URI object. Any URL which compiles with RFC 2396 can be converted to URI. URLs which are not in the specified format will generate an error if converted to URI format. Function Signature public URI toURI() Syntax url.toURI() Parameter: 2 min read URL getRef() method in Java with Examples The getRef() function is a part of URL class. The function getRef() returns the Reference or anchor part of a specified URL. Function Signature: public String getRef() Syntax: url.getRef() Parameter: This function does not require any parameter Return Type: The function returns String Type Below pro 2 min read URL getFile() method in Java with Examples The getFile() function is a part of URL class. The function getFile() returns the file name of a specified URL. The getFile() function returns the path and the query of the URL. Function Signature: public String getFile() Syntax: url.getFile() Parameter: This function does not require any parameter 2 min read Like