Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

ResourceLoader



The ResourceLoader class allows to retrieve resources in an application or library Jar files.

Basic usage

The basic usage allows to construct the ResourceLoader with the package, and get resources in this package.

By default the resources will be retrieved using the context Class Loader of the current Thread.

Example

For example, suppose that we have the following class structure:
      ==> my
      ====> resources
      ======> resource.xml
      ====> package
      ======> MyClass.java
We can use the following code to retrieve the resource.xml resource in the MyClass class:
   public class MyClass {
     public static void main(String[] args) {
       ResourceLoader loader = new ResourceLoader("my/resources");
       URL resourceURL = loader.getURL("resource.xml");
     }
   }

   ClassLoader parentLoader = Thread.currentThread().getContextClassLoader();
   URLClassLoader loader = new URLClassLoader(urls, parentLoader);

   ResourceLoader loader = new ResourceLoader("my/package", loader);
   URL resourceURL = loader.getURL("resource.xml");

ClassLoader argument

By default the ClassLoader used to retrieve the resources is the current ContextClassLoader. However one of the constructors allows to create the class and specify which ClassLoader to use.

It is important to consider that if you try to retrieve resources in jar files which are not part of the application or library ClassPath (such as jar files loaded at runtime), you will need to create an URLClassLoader to load these resources, because the original ContextClassLoader won't be able to load the resources in this jar file.

Example

For example, suppose that we have a Jar File loaded at runtime with the following structure:
      theJarFile.jar:
      ==> the
      ====> package
      ======> resource.xml
We could use the following code to retrieve the resource.xml resource:
   URL url = theJarFile.toURI().toURL();
   URL[] urls = new URL[1]
   urls[0] = url;

   ClassLoader parentLoader = Thread.currentThread().getContextClassLoader();
   URLClassLoader loader = new URLClassLoader(urls, parentLoader);

   ResourceLoader loader = new ResourceLoader("the/package", loader);
   URL resourceURL = loader.getURL("resource.xml");

Using a Class or an Object to get the ClassLoader

The constructors and methods of the ResourceLoader class allow to use a Class or an Object as argument. The associated ClassLoader will be used instead of the context ClassLoader.

Example

For example, suppose that we have a Jar File loaded at runtime with the following structure:
      theJarFile.jar:
      ==> the
      ====> package
      ======> resource.xml
For example with a Class:
   ResourceLoader loader = new ResourceLoader("the/package", MyClass.class);
   URL url loader.getURL("resource.xml");
or:
   ResourceLoader loader = new ResourceLoader("the/package");
   URL url loader.getURL("resource.xml", MyClass.class));

Inferring the ClassLoader from the context

It is possible to infer the ClassLoader from the context using the ResourceLoader.createFromContext(String, boolean) static method.
  • If the argument is true, the sun.reflect.Reflection will be used internally
  • If the argument is false, the code will look for the ClassLoader which had loaded the caller class. The path of the caller class will be retrieved through the StackTrace

Note that this method can has an adverse effect on performance, especially if the argument is set to false.

Categories: lang | packages

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences