public final class Resources extends Object
The commonly used methods to load resources might differ when running from JAR instead of running from local file system. These methods make sure behavior is the same and code works in both situations.
Modifier and Type | Method and Description |
---|---|
static Optional<Path> |
getPathToClass(Class<?> clazz)
Determine the class file that
clazz is defined in. |
static Optional<Path> |
getPathToJar(Class<?> clazz)
Determine the JAR file that
clazz is loaded from. |
static Optional<Path> |
getResource(ClassLoader classLoader,
Path resource)
Finds the path to
resource using the class loader's resource lookup
algorithm. |
static Optional<Path> |
getResourceRelativeTo(Class<?> clazz,
Path resource)
Finds the path to
resource relative to clazz . |
static Stream<Path> |
getResources(ClassLoader classLoader,
Path resource)
Finds the paths to
resource using the class loader's resource lookup
algorithm. |
static Stream<Path> |
getResourcesRelativeTo(Class<?> clazz,
Path resource)
Finds the paths to
resource relative to clazz . |
static Optional<Manifest> |
readManifest(Class<?> clazz)
Reads the
Manifest of the JAR file that clazz has been loaded
from. |
public static Optional<Path> getPathToClass(Class<?> clazz)
clazz
is defined in. Classes with no
own class file result in Optional.empty()
.
This method might load (without closing) a FileSystem
to handle a JAR archive.
Attention: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8131067
clazz
- classpublic static Optional<Path> getPathToJar(Class<?> clazz)
clazz
is loaded from.
Classes outside of a JAR or with no own class file result in
Optional.empty()
.
clazz
- classpublic static Optional<Path> getResource(ClassLoader classLoader, Path resource)
resource
using the class loader's resource lookup
algorithm.
The commonly used method ClassLoader.getResource(String)
might differ
when running from JAR instead of running from local file system. This method
makes sure behavior is the same and code works in both situations.
Calling this might load the FileSystem
that is
registered for handling JAR files. If no file or folder is found
Optional.empty()
is returned.
classLoader
- class loader to use for resource lookupresource
- path to the resource to findpublic static Optional<Path> getResourceRelativeTo(Class<?> clazz, Path resource)
resource
relative to clazz
.
The commonly used method Class.getResource(String)
might differ when
running from JAR instead of running from local file system. This method makes
sure behavior is the same and code works in both situations.
Calling this might load the FileSystem
that is
registered for handling JAR files. If no file or folder is found
Optional.empty()
is returned.
clazz
- class to use for resource lookupresource
- relative path to the resource to findpublic static Stream<Path> getResources(ClassLoader classLoader, Path resource)
resource
using the class loader's resource lookup
algorithm. Instead of getResource(ClassLoader, Path)
this method
returns not only the first matching resource, but also the shadowed resources
based on the class loaders hierarchy.
The commonly used method ClassLoader.getResources(String)
might
differ when running from JAR instead of running from local file system. This
method makes sure behavior is the same and code works in both situations.
Calling this might load the FileSystem
that is
registered for handling JAR files. If no file or folder is found
Optional.empty()
is returned.
classLoader
- class loader to use for resource lookupresource
- path to the resource to findpublic static Stream<Path> getResourcesRelativeTo(Class<?> clazz, Path resource)
resource
relative to clazz
. Instead of
getResourceRelativeTo(Class, Path)
this method returns not only the
first matching resource, but also the shadowed resources based on the class
loaders hierarchy.
Calling this might load the FileSystem
that is
registered for handling JAR files. If no file or folder is found
Optional.empty()
is returned.
clazz
- class to use for resource lookupresource
- path to the resource to findpublic static Optional<Manifest> readManifest(Class<?> clazz) throws IOException
Manifest
of the JAR file that clazz
has been loaded
from.
Classes outside of a JAR or with no own class file or JARs with no manifest
result in Optional.empty()
.
clazz
- classIOException
- if an I/O error occursCopyright © 2025. All rights reserved.