Finding and reading resources using a classloader
suggest changeResource loading in Java comprises the following steps:
- Finding the
ClassorClassLoaderthat will find the resource. - Finding the resource.
- Obtaining the byte stream for the resource.
- Reading and processing the byte stream.
- Closing the byte stream.
The last three steps are typically accomplished by passing the URL to a library method or constructor to load the resource. You will typically use a getResource method in this case. It is also possible to read the resource data in application code. You will typically use getResourceAsStream in this case.
Absolute and relative resource paths
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. A relative path starts with a name, and an absolute path starts with a separator.
As the Classpath examples describe, a JVM’s classpath defines a namespace by overlaying the namespaces of the directories and JAR or ZIP files in the classpath. When an absolute path is resolved, the class loaders interpret the initial / as the root of the namespace. A ClassLoader path should not start with /; that method treats the slash as part of the name. By contrast, a relative path may be resolved relative to any “folder” in the namespace. The folder used will depend on the object that you use to resolve the path.
Obtaining a Class or Classloader
A resource can be located using either a Class object or a ClassLoader object. A Class object can resolve relative paths, so you will typically use one of these if you have a (class) relative resource. There are a variety of ways to obtain a Class object. For example:
- A class literal will give you the
Classobject for any class that you can name in Java source code; e.g.String.classgives you theClassobject for theStringtype. Object.getClass()gives you theClassobject for the type of any object; e.g."hello".getClass()is another way to get theClassofString.- The
Class.forName(String)method will (if necessary) dynamically load a class and return itsClassobject; e.g.Class.forName("java.lang.String").
A ClassLoader object is typically obtained by calling getClassLoader() on a Class object. It is also possible to get hold of the JVM’s default classloader using the static ClassLoader.getSystemClassLoader() method.
The get methods
Once you have a Class or ClassLoader instance, you can find a resource, using one of the following methods:
| Methods | Description |
|---|---|
Class.getResource(path), ClassLoader.getResource(path) |
Returns one URL for that path, or null. |
ClassLoader.getResources(path) |
Returns an Enumeration<URL>. Class has no getResources method. |
Class.getResourceAsStream(path), ClassLoader.getResourceAsStream(path) |
Returns an InputStream, or null. There is no Class.getResourceStream. |
Notes:
-
The main difference between the
ClassLoaderandClassversions of the methods is in the way that relative paths are interpreted. -
The
Classmethods resolve a relative path in the folder that corresponds to the class's package. -
The
ClassLoadermethods treat relative paths as if they were absolute; i.e. they resolve them in the root folder of the classpath namespace. -
If the requested resource cannot be found,
getResourceandgetResourceAsStreamreturnnull, andgetResourcesreturns an emptyEnumeration. -
The URLs can be opened with
URL.openStream(). They may befile:URLs or other ordinary URLs. A resource inside a JAR is ajar:URL that identifies the JAR and the entry. -
If your code uses
getResourceAsStream(orURL.openStream()) to obtain anInputStream, it is responsible for closing that stream. Failure to close the stream can leak the underlying file or JAR entry.