Tuesday, October 7, 2025
HomeLanguagesJavaURL getPath() method in Java with Examples

URL getPath() method in Java with Examples

The getPath() function is a part of URL class. The function getPath() returns the Path name of a specified URL.

Function Signature:

public String getPath()

Syntax:

url.getPath()

Parameter: This function does not require any parameter
Return Type: The function returns String Type

Below programs illustrates the use of getPath() function:

Example 1: Given a URL we will get the Path using the getPath() function.




// Java program to show the
// use of the function getPath()
  
import java.net.*;
  
class Solution {
    public static void main(String args[])
    {
        // url  object
        URL url = null;
  
        try {
            // create a URL
            url = new URL(
                "https:// www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples/");
  
            // get the  Path
            String _Path = url.getPath();
  
            // display the URL
            System.out.println("URL = " + url);
  
            // display the  Path
            System.out.println(" Path= " + _Path);
        }
        // if any error occurs
        catch (Exception e) {
  
            // display the error
            System.out.println(e);
        }
    }
}


Output:

URL = https:// www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples/
 Path= /url-getprotocol-method-in-java-with-examples/

Example 2: Now see how getPath() is different from getFile(). getPath() will exclude the query but getFile() will include the query




// Java program to show the
// use of the function getPath()
  
import java.net.*;
  
class Solution {
    public static void main(String args[])
    {
        // url  object
        URL url = null;
  
        try {
            // create a URL
            url = new URL(
                "https:// www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol");
  
            // get the  Path
            String _Path = url.getPath();
  
            // display the URL
            System.out.println("URL = " + url);
  
            // display the  Path
            System.out.println(" Path= " + _Path);
        }
        // if any error occurs
        catch (Exception e) {
  
            // display the error
            System.out.println(e);
        }
    }
}


Output:

URL = https:// www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol
 Path= /url-getprotocol-method-in-java-with-examples
RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS