The javadoc for Class.getResourceAsStream() documents the lookup logic:

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.

Otherwise, the absolute name is of the following form:
modified_package_name/name

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

So in other words, the resource name passed to the method should look like /com/package/p2/props.properties if the props.properties is stored in the com.package.p2 package instead of the current class's.

Answer from matt b on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › loading-resources-from-classpath-in-java-with-example
Loading Resources from Classpath in Java with Example - GeeksforGeeks
July 23, 2025 - Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
🌐
Blogger
javarevisited.blogspot.com › 2014 › 07 › how-to-load-resources-from-classpath-in-java-example.html
How to Load Resources from Classpath in Java with Example
A blog about Java, Programming, Algorithms, Data Structure, SQL, Linux, Database, Interview questions, and my personal experience.
🌐
Mkyong
mkyong.com › home › java › java – read a file from resources folder
Java - Read a file from resources folder - Mkyong.com
September 4, 2020 - This article will show you how to read a file from a `resources` folder, `getResourceAsStream` or `getResource`.
🌐
Xenovation
xenovation.com › home › blog › java › java - read file from classpath
Java - Read file from classpath | XENOVATION
April 24, 2019 - Learn how to read a file from the java classpath (resources mostly inside a jar, target or bin folder)
🌐
TutorialsPoint
tutorialspoint.com › loading-resources-from-classpath-in-java-with-example
Loading Resources from classpath in Java with Example
July 28, 2023 - Resources play an important role during runtime by providing necessary files that enable smooth software operation. These resources are accessible through a classpath system which allows them to be read from or written into with ease. For managing th
Find elsewhere
🌐
Java Code Geeks
javacodegeeks.com › home › core java
How to Load Resources from Classpath in Java with Example
August 1, 2014 - Classpath in Java is not only used to load .class files, but also can be used to load resources e.g. properties file, images, icons, thumbnails, or any
🌐
LogicBig
logicbig.com › how-to › java › different-ways-to-load-resources.html
What are different ways to load classpath resources in Java?
November 4, 2018 - We just need to add a pom and put ... src/main/java (see quick tutorials on maven here): We can see there are no resources copied because we have to place all our txt files in the 'resources' folder. Here's the complete maven project with all txt files in the 'resources' folder: ... C:\load-resource>mvn ...
🌐
Coderanch
coderanch.com › t › 588460 › java › Loading-resource-jar-classpath
Loading resource from different jar on classpath (Java in General forum at Coderanch)
I have a problem loading resource file from external jar file. Is there a way to access these resources placed in schemas.jar from execution.jar using classpath identifiers?
Top answer
1 of 14
368

Intro and basic Implementation

First up, you're going to need at least a URLStreamHandler. This will actually open the connection to a given URL. Notice that this is simply called Handler; this allows you to specify java -Djava.protocol.handler.pkgs=org.my.protocols and it will automatically be picked up, using the "simple" package name as the supported protocol (in this case "classpath").

Usage

new URL("classpath:org/my/package/resource.extension").openConnection();

Code

package org.my.protocols.classpath;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

/** A {@link URLStreamHandler} that handles resources on the classpath. */
public class Handler extends URLStreamHandler {
    /** The classloader to find resources from. */
    private final ClassLoader classLoader;

    public Handler() {
        this.classLoader = getClass().getClassLoader();
    }

    public Handler(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    @Override
    protected URLConnection openConnection(URL u) throws IOException {
        final URL resourceUrl = classLoader.getResource(u.getPath());
        return resourceUrl.openConnection();
    }
}

Launch issues

If you're anything like me, you don't want to rely on a property being set in the launch to get you somewhere (in my case, I like to keep my options open like Java WebStart - which is why I need all this).

Workarounds/Enhancements

Manual code Handler specification

If you control the code, you can do

new URL(null, "classpath:some/package/resource.extension", new org.my.protocols.classpath.Handler(ClassLoader.getSystemClassLoader()))

and this will use your handler to open the connection.

But again, this is less than satisfactory, as you don't need a URL to do this - you want to do this because some lib you can't (or don't want to) control wants urls...

JVM Handler registration

The ultimate option is to register a URLStreamHandlerFactory that will handle all urls across the jvm:

package my.org.url;

import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
import java.util.HashMap;
import java.util.Map;

class ConfigurableStreamHandlerFactory implements URLStreamHandlerFactory {
    private final Map<String, URLStreamHandler> protocolHandlers;

    public ConfigurableStreamHandlerFactory(String protocol, URLStreamHandler urlHandler) {
        protocolHandlers = new HashMap<String, URLStreamHandler>();
        addHandler(protocol, urlHandler);
    }

    public void addHandler(String protocol, URLStreamHandler urlHandler) {
        protocolHandlers.put(protocol, urlHandler);
    }

    public URLStreamHandler createURLStreamHandler(String protocol) {
        return protocolHandlers.get(protocol);
    }
}

To register the handler, call URL.setURLStreamHandlerFactory() with your configured factory. Then do new URL("classpath:org/my/package/resource.extension") like the first example and away you go.

JVM Handler Registration Issue

Note that this method may only be called once per JVM, and note well that Tomcat will use this method to register a JNDI handler (AFAIK). Try Jetty (I will be); at worst, you can use the method first and then it has to work around you!

License

I release this to the public domain, and ask that if you wish to modify that you start a OSS project somewhere and comment here with the details. A better implementation would be to have a URLStreamHandlerFactory that uses ThreadLocals to store URLStreamHandlers for each Thread.currentThread().getContextClassLoader(). I'll even give you my modifications and test classes.

2 of 14
117
URL url = getClass().getClassLoader().getResource("someresource.xxx");

That should do it.

🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › guides › lang › resources.html
Location-Independent Access to Resources
April 21, 2026 - Methods in the classes Class and ... For example, they enable locating resources for: An applet loaded from the Internet using multiple HTTP connections. An applet loaded using JAR files. A Java Bean loaded or installed in the CLASSPATH....
🌐
DevTut
devtut.github.io › java › resources-on-classpath.html
Java - Resources (on classpath)
Resources that can be loaded from the classpath are denoted by a path. The syntax of the path is similar to a UNIX / Linux file path. It consists of simple names separated by forward slash (/) characters.
🌐
Rip Tutorial
riptutorial.com › resources (on classpath)
Java Language Tutorial => Resources (on classpath)
Java allows the retrieval of file-based resources stored inside of a JAR alongside compiled classes. This topic focuses on loading those resources and making them available to your code. A resource is file-like data with a path-like name, which resides in the classpath.
🌐
Spring
docs.spring.io › spring-framework › docs › 3.0.6.RELEASE_to_3.1.0.BUILD-SNAPSHOT › 3.0.6.RELEASE › org › springframework › core › io › ClassPathResource.html
ClassPathResource
Supports resolution as java.io.File if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL. ... Create a new ClassPathResource for ClassLoader usage. A leading slash will be removed, as the ClassLoader resource access methods will not accept it. The thread context class loader ...
🌐
Blogger
javarevisited.blogspot.com › 2014 › 07 › how-to-load-resources-from-classpath-in-java-example.html
Javarevisited: How to Load Resources from Classpath in Java with Example
A blog about Java, Programming, Algorithms, Data Structure, SQL, Linux, Database, Interview questions, and my personal experience.
🌐
Spring
docs.spring.io › spring-framework › docs › current › javadoc-api › org › springframework › core › io › ClassPathResource.html
ClassPathResource (Spring Framework 7.0.7 API)
Create a new ClassPathResource for Class usage. The path can be relative to the given class, or absolute within the class path via a leading slash. If the supplied Class is null, the default class loader will be used for loading the resource. This is also useful for resource access within the module system, loading a resource from the containing module of a given Class. See ModuleResource and its javadoc...