Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:


1. Overview

In this short tutorial, we’ll look at the differences between the Class.getResource() and ClassLoader.getResource() methods.

2. The getResource() Method

We can use the getResource() method on either a Class or ClassLoader instance to find a resource with the given name. The resource is considered to be data — for instance, images, text, audio, and so on. As a path separator, we should always use a slash (“/”).

The method returns a URL object for reading the resource, or the null value if the resource cannot be found or the invoker doesn’t have privileges to retrieve the resource.

3. Class.getResource()

Now, let’s see how we can get the resource using a Class instance. We can pass an absolute or relative path when locating a resource with a Class object.

The rules for searching resources associated with a given class are implemented by the class loader of the class.

The process of finding the resource will be delegated to the class object’s class loader. In other words, the getResouce() method defined on the Class instance will eventually call ClassLoader‘s getResouce() method.

Before delegation, an absolute resource name will be derived from the given resource name. When creating the absolute resource name, the following algorithm will be used:

  • If the resource name starts with a leading slash (“/”), it indicates the resource name is absolute. Absolute resource names are cleaned of their leading slash and are passed without any modification to the appropriate ClassLoader method to locate the resource.
  • If the provided resource name doesn’t start with a slash, the name is treated as relative to the class’s package. Relative names are first transformed to absolute and then are passed to the ClassLoader method.

Firstly, let’s suppose we have the example.txt resource defined inside the com/baeldung/resource directory. Furthermore, let’s assume we have the class ClassGetResourceExample defined inside the com.baeldung.resource package.

Now, we can retrieve the resource using the absolute path:

void givenAbsoluteResourcePath_whenGetResource_thenReturnResource() {
    URL resourceAbsolutePath = ClassGetResourceExample.class

When using Class.getResource(), absolute resource paths should begin with the leading slash.

Additionally, since our resource is inside the same package as our class, we can retrieve it using a relative path as well:

void givenRelativeResourcePath_whenGetResource_thenReturnResource() {
    URL resourceRelativePath = ClassGetResourceExample.class.getResource("example.txt");

However, it’s important to mention that we can get the resource using a relative path only if the resource is defined inside the same package as a class. Otherwise, we’ll get a null as a value.

4. ClassLoader.getResource()

As the name suggests, the ClassLoader represents a class responsible for loading classes. Every Class instance contains a reference to its ClassLoader.

The ClassLoader class uses a delegation model to search for classes and resources. Additionally, each instance of the ClassLoader class has an associated parent ClassLoader.

When asked to find a resource, the ClassLoader instance will first delegate the search to its parent ClassLoader before attempting to find the resource itself.

In case a parent ClassLoader doesn’t exist, the path of the virtual machine’s built-in ClassLoader, called the bootstrap class loader, is searched. The bootstrap class loader doesn’t have a parent but may serve as a parent of a ClassLoader instance.

Alternatively, the method will invoke the findResource() method to find the resource if the previous searches have failed.

The resource name specified as an input is always considered to be absolute. It’s important to note that Java loads resources from the classpath.

Let’s get the resource using the absolute path and the ClassLoader instance:

void givenAbsoluteResourcePath_whenGetResource_thenReturnResource() {
    URL resourceAbsolutePath = ClassLoaderGetResourceExample.class.getClassLoader()

When we call ClassLoader.getResource(), we should omit the leading slash when defining the absolute path.

Using the ClassLoader instance, we cannot get resources using the relative path:

void givenRelativeResourcePath_whenGetResource_thenReturnNull() {
    URL resourceRelativePath = ClassLoaderGetResourceExample.class.getClassLoader()

The test above demonstrates that the method returns a null value as a result.

5. Conclusion

This short tutorial explained the differences between calling the getResource() method from the Class and ClassLoader instances. To sum up, we can pass either a relative or an absolute resource path when calling the method using the Class instance, but we can only use the absolute path when calling the method on the ClassLoader.

As always, the code is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.