java.lang.Object
imagingbook.core.resource.ResourceUtils
This class defines static methods for accessing resources. What makes things somewhat complex is the requirement that we want to retrieve resources located in the file system or contained inside a JAR file.
Here is a typical URI for a JAR-embedded file:
"jar:file:/C:/PROJEC~2/parent/IM1D84~1/ImageJ/jars/jarWithResources.jar!/jarWithResouces/resources/clown.jpg"
- Version:
- 2022/11/01
-
Method Summary
Modifier and TypeMethodDescriptionstatic String[]
getResourceFileNames
(Class<?> clazz, String relDir) Use this method to obtain the names of all files in a directory located relative to the specified class (non-recursively).static Path
getResourcePath
(Class<?> clazz, String relPath) Finds the path to a resource relative to the location of some class.static Path[]
getResourcePaths
(Class<?> clazz, String relPath) Use this method to obtain the paths to all files in a directory located relative to the specified class (non-recursively).static Path[]
getResourcePaths
(URI uri) static Path[]
getResourcePaths
(Path path) Method to obtain the paths to all files in a directory specified by aPath
(non-recursively).static URI
getResourceUri
(Class<?> clazz, String relPath) Finds the URI for a resource relative to a specified class.static boolean
isInsideJar
(Class<?> clazz) Determines if the specified class was loaded from a JAR file or a .class file in the file system.static Path
-
Method Details
-
isInsideJar
Determines if the specified class was loaded from a JAR file or a .class file in the file system.- Parameters:
clazz
- the class- Returns:
- true if contained in a JAR file, false otherwise
-
getResourceUri
Finds the URI for a resource relative to a specified class. The resource may be located in the file system or inside a JAR file.- Parameters:
clazz
- the anchor classrelPath
- the resource path relative to the anchor class- Returns:
- the URI or
null
if the resource was not found
-
getResourcePath
Finds the path to a resource relative to the location of some class. Example: Assume class C was loaded from file
someLocation/C.class
and there is a subfoldersomeLocation/resources/
that contains an image filelenna.jpg
. Then the complete path to this image is obtained byPath path = getResourcePath(C.class, "resources/lenna.jpg");
- Parameters:
clazz
- anchor classrelPath
- the path of the resource to be found (relative to the location of the anchor class)- Returns:
- the path to the specified resource
-
uriToPath
- Parameters:
uri
- the specified location- Returns:
- the associated path
-
getResourcePaths
-
getResourcePaths
Method to obtain the paths to all files in a directory specified by aPath
(non-recursively). This should work in an ordinary file system as well as a (possibly nested) JAR file.- Parameters:
path
-Path
to a directory (may be contained in a JAR file)- Returns:
- a possibly empty sequence of paths
-
getResourcePaths
Use this method to obtain the paths to all files in a directory located relative to the specified class (non-recursively). This should work in an ordinary file system as well as a (possibly nested) JAR file.- Parameters:
clazz
- class whose source location defines the rootrelPath
- path relative to the root- Returns:
- a possibly empty array of paths
-
getResourceFileNames
Use this method to obtain the names of all files in a directory located relative to the specified class (non-recursively). This should work in an ordinary file system as well as a (possibly nested) JAR file.- Parameters:
clazz
- class whose source location specifies the rootrelDir
- directory relative to the root- Returns:
- a possibly empty array of file names
-