Java URL Class

Last Updated : 5 Feb, 2026

The Java URL class, available in the java.net package, is used to represent and work with Uniform Resource Locators (URLs). It helps Java programs identify internet resources and access their components such as protocol, host, and path.

  • Used to locate and access internet resources
  • Supports protocols like HTTP, HTTPS, and FTP

Components of a URL

A URL can have many forms. The most general however follows a three-component system as proposed below:

URL-Class
  • Protocol: "http" is the protocol here.
  • Host Machine: Name of the machine on which the resource lives (www.geeksforgeeks.org)
  • File Name: The pathname to the file on the machine.
  • Port Number: Port number to which to connect (typically optional).

URL Class 

The URL class is a core Java networking class used to access internet-based resources.

  • It can point to static files, dynamic content, and APIs
  • Provides constructors to create URL objects
  • Offers methods to extract different parts of a URL

Constructors of URL class

1. URL(String address): It creates a URL object from the specified String.

URL url = new URL("https://www.example.com/");

2. URL(String protocol, String host, String file): Creates a URL object from the specified protocol, host, and file name.

URL url = new URL("http", "www.example.com", "/path/to/resource");

3. URL(String protocol, String host, int port, String file): Creates a URL object from protocol, host, port, and file name.

URL url = new URL("https", "www.example.com", 443, "/path/to/resource");

4. URL(URL context, String spec): Creates a URL object by parsing the given spec in the given context. 

URL baseUrl = new URL("https://www.example.com/");
URL relativeUrl = new URL(baseUrl, "/path/to/resource");

5. URL(String protocol, String host, int port, String file, URLStreamHandler handler): Creates a URL object from the specified protocol, host, port number, file, and handler.

URL url = new URL("http", "www.example.com", 80, "/path/to/resource", new MyCustomHandler());

6. URL(URL context, String spec, URLStreamHandler handler): Creates a URL by parsing the given spec with the specified handler within a specified context.

URL baseUrl = new URL("https://www.example.com/");
URL relativeUrl = new URL(baseUrl, "/path/to/resource", new MyCustomHandler());

Java URL Methods

MethodDescription
getAuthority()Returns the authority part of URL or null if empty
getDefaultPort()Returns the default port used
getFile()Returns the file name.
getHost()Return the hostname of the URL in IPv6 format
getPath()Returns the path of the URL, or null if empty
getPort()Returns the port associated with the protocol specified by the URL
getProtocol()Returns the protocol used by the URL
getQuery()Return the query part of the URL, which is the portion following the ? character, used to pass parameters to a web application.
getRef()Return the reference part of the URL, which is the portion following the # character, typically used to navigate to a specific section within a web page.
toString()As in any class, toString() returns the string representation of the given URL object.

Example: Program to demonstrate the working of URL.

Java
import java.net.MalformedURLException;
import java.net.URL;

public class Geeks {

    public static void main(String[] args)
        throws MalformedURLException
    {
        // Creating a URL with string representation
        URL u1 = new URL(
            "https://www.google.co.in/?gfe_rd=cr&ei=ptYq"
            + "WK26I4fT8gfth6CACg#q=geeks+for+geeks+java");

        // Creating a URL with a protocol, hostname, and
        // path
        URL u2 = new URL("http", "www.geeksforgeeks.org",
                         "/jvm-works-jvm-architecture/");

        URL u3 = new URL(
            "https://www.google.co.in/search"
            + "q=gnu&rlz=1C1CHZL_enIN714IN715&oq=gnu&aqs=chrome..69i57j69i60l5.653j0j7&sourceid=chrome&ie=UTF-8#q=geeks+for+geeks+java");

        // Printing the string representation of the URL
        System.out.println(u1.toString());
        System.out.println(u2.toString());
        System.out.println();
        System.out.println("Different components of URL3:");

        // Retrieving the protocol for the URL
        System.out.println("Protocol: " + u3.getProtocol());

        // Retrieving the hostname of the URL
        System.out.println("Hostname: " + u3.getHost());

        // Retrieving the default port
        System.out.println("Default port: "
                           + u3.getDefaultPort());

        // Retrieving the query part of the URL
        System.out.println("Query: " + u3.getQuery());

        // Retrieving the path of the URL
        System.out.println("Path: " + u3.getPath());

        // Retrieving the file name
        System.out.println("File: " + u3.getFile());

        // Retrieving the reference
        System.out.println("Reference: " + u3.getRef());
    }
}

Output:

Output
Comment