Load resource from anywhere in classpath

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

If the name begins with a "https://stackoverflow.com/" ('\u002f'), then the absolute name of the resource is the portion of the name following the “https://stackoverflow.com/”.

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 “https://stackoverflow.com/” 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.

Leave a Comment