FileDocCategorySizeDatePackage
ClassLoaderUtils.javaAPI DocExample5133Mon Jul 23 13:26:56 BST 2007org.apache.struts2.util

ClassLoaderUtils

public class ClassLoaderUtils extends Object
This class is extremely useful for loading resources and classes in a fault tolerant manner that works across different applications servers.

It has come out of many months of frustrating use of multiple application servers at Atlassian, please don't change things unless you're sure they're not going to break in one server or another!

Fields Summary
Constructors Summary
Methods Summary
public static java.net.URLgetResource(java.lang.String resourceName, java.lang.Class callingClass)
Load a given resource.

This method will try to load the resource using the following methods (in order):

  • From {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()}
  • From {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()}
  • From the {@link Class#getClassLoader() callingClass.getClassLoader() }

param
resourceName The name of the resource to load
param
callingClass The Class object of the calling object

        URL url = null;

        url = Thread.currentThread().getContextClassLoader().getResource(resourceName);

        if (url == null) {
            url = ClassLoaderUtils.class.getClassLoader().getResource(resourceName);
        }

        if (url == null) {
            url = callingClass.getClassLoader().getResource(resourceName);
        }

        return url;
    
public static java.io.InputStreamgetResourceAsStream(java.lang.String resourceName, java.lang.Class callingClass)
This is a convenience method to load a resource as a stream.

The algorithm used to find the resource is given in getResource()

param
resourceName The name of the resource to load
param
callingClass The Class object of the calling object

        URL url = getResource(resourceName, callingClass);

        try {
            return (url != null) ? url.openStream() : null;
        } catch (IOException e) {
            return null;
        }
    
public static java.lang.ClassloadClass(java.lang.String className, java.lang.Class callingClass)
Load a class with a given name.

It will try to load the class in the following order:

  • From {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()}
  • Using the basic {@link Class#forName(java.lang.String) }
  • From {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()}
  • From the {@link Class#getClassLoader() callingClass.getClassLoader() }

param
className The name of the class to load
param
callingClass The Class object of the calling object
throws
ClassNotFoundException If the class cannot be found anywhere.

        try {
            return Thread.currentThread().getContextClassLoader().loadClass(className);
        } catch (ClassNotFoundException e) {
            try {
                return Class.forName(className);
            } catch (ClassNotFoundException ex) {
                try {
                    return ClassLoaderUtils.class.getClassLoader().loadClass(className);
                } catch (ClassNotFoundException exc) {
                    return callingClass.getClassLoader().loadClass(className);
                }
            }
        }
    
public static voidprintClassLoader()
Prints the current classloader hierarchy - useful for debugging.

        System.out.println("ClassLoaderUtils.printClassLoader");
        printClassLoader(Thread.currentThread().getContextClassLoader());
    
public static voidprintClassLoader(java.lang.ClassLoader cl)
Prints the classloader hierarchy from a given classloader - useful for debugging.

        System.out.println("ClassLoaderUtils.printClassLoader(cl = " + cl + ")");

        if (cl != null) {
            printClassLoader(cl.getParent());
        }