Loading Resources from Classpath in Java with Example - GeeksforGeeks

Loading Resources from Classpath in Java with Example

Last Updated : 3 Sep, 2026

Resources are files such as text files, images, configuration files, and other data that an application can access at runtime. Java provides methods to load these resources from the classpath and perform operations such as reading their contents.

  • A resource must be available on the application's classpath.
  • In a standard project, resources are commonly placed in src/main/resources.

Example: An application may need to load a text file that is packaged with the application. Instead of specifying an absolute file-system path, the file can be loaded using its location on the classpath

Directory Structure

Before running the examples, create the files using the following structure:

dire
Directory Structure

Here:

  • GFG2.java contains the Java program.
  • GFG_text.txt is the resource file that we want to load.
  • src/main/resources contains resources that are included in the application's classpath.

How Classpath Resource Loading Works

The basic process of loading a resource is:

  • Place the resource file on the classpath.
  • Specify the resource name or path.
  • Use getResource() or getResourceAsStream() to locate the resource.
  • getResource() returns a URL, while getResourceAsStream() returns an InputStream.
  • Read or process the resource using the returned object.

Different Ways to Load Resources from Classpath

1. Using getResourceAsStream() Method

The getResourceAsStream() method is used to load a resource as an InputStream. This stream can then be used to read the contents of the resource.

Example: The resource file is named GFG_text.txt and is placed in the classpath.

Java
import java.io.*;

public class GFG2 {

    public static void main(String[] args) throws Exception {

        // Name of the resource
        String fileName = "GFG_text.txt";

        System.out.println("Getting the data of file " + fileName);

        // Loading the resource as an InputStream
        InputStream instr =
                GFG2.class.getClassLoader()
                        .getResourceAsStream(fileName);

        // Check whether the resource was found
        if (instr == null) {
            System.out.println("Resource not found: " + fileName);
            return;
        }

        // Reading the resource using BufferedReader
        BufferedReader rr =
                new BufferedReader(new InputStreamReader(instr));

        String line;

        // Reading each line of the file
        while ((line = rr.readLine()) != null) {
            System.out.println(line);
        }

        rr.close();
    }
}

Output:

output of the above program.

Explanation:

  • GFG2.class refers to the GFG2 class.
  • getClassLoader() obtains the class loader.
  • getResourceAsStream() searches for GFG_text.txt on the classpath.
  • It returns an InputStream when the resource is found.
  • BufferedReader is used to read the file line by line.
  • If the resource is not found, getResourceAsStream() returns null.

Note: getResourceAsStream() is useful when the main requirement is to read the resource as a stream.

2. Using getResource() Method

The getResource() method is used to locate a resource on the classpath and returns its location as a URL.

Java
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;

//save file as the name of GFG2
public class GFG2 {
  
    //main class
    public static void main(String[] args) throws Exception {
      
        // creating object of the class
        // important since the getClass() method is 
        // not static.
        GFG2 obj = new GFG2();
      
        // name of the resource
        // the resource is stored in our base path of the 
        // .class file.
        String fileName = "GFG_text.txt";
        
        System.out.println("Getting the data of file " + fileName);
      
        // getting the URL of the resource
        // and creating a file object to the given URL
        URL url = obj.getClass().getClassLoader().getResource(fileName);
      
        File file = new File(url.toURI());
      
        // reading the file data 
        // by creating a list of strings 
        // of each line
        List<String> line;
      
        // method of files class to read all the lines of the 
        // file specified.
        line = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
      
        // reading the list of the line in the file.
        for(String s: line)
            System.out.println(s);
    }
}

Output

output for the second code.

Explanation

  • GFG2.class.getClassLoader() obtains the class loader.
  • getResource() searches for GFG_text.txt on the classpath.
  • It returns a URL if the resource is found.
  • If the resource is not found, it returns null.
  • The returned URL can be used to locate the resource.

Note: A classpath resource should not always be converted directly into a File. This may work when the resource is available as a normal file, but may not work when the application is packaged inside a JAR. When the goal is simply to read a resource, getResourceAsStream() is generally more suitable.

Advantages

  • Resources can be easily accessed from the classpath.
  • It avoids using fixed or absolute file paths.
  • Resources can be packaged with the application.
  • It keeps resource files separate from Java source files.
  • The application can access resources from different environments.
  • getResource() and getResourceAsStream() provide simple ways to load resources.
Comment