Load a given resource.
import java.net.URL; //Revised from apache cxf public class Main { /** * Load a given resource. <p/> This method will try to load the resource * using the following methods (in order): * <ul> * <li>From Thread.currentThread().getContextClassLoader() * <li>From ClassLoaderUtil.class.getClassLoader() * <li>callingClass.getClassLoader() * </ul> * * @param resourceName The name of the resource to load * @param callingClass The Class object of the calling object */ public static URL getResource(String resourceName, Class callingClass) { URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName); if (url == null && resourceName.startsWith("/")) { //certain classloaders need it without the leading / url = Thread.currentThread().getContextClassLoader() .getResource(resourceName.substring(1)); } ClassLoader cluClassloader = Main.class.getClassLoader(); if (cluClassloader == null) { cluClassloader = ClassLoader.getSystemClassLoader(); } if (url == null) { url = cluClassloader.getResource(resourceName); } if (url == null && resourceName.startsWith("/")) { //certain classloaders need it without the leading / url = cluClassloader.getResource(resourceName.substring(1)); } if (url == null) { ClassLoader cl = callingClass.getClassLoader(); if (cl != null) { url = cl.getResource(resourceName); } } if (url == null) { url = callingClass.getResource(resourceName); } if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) { return getResource('/' + resourceName, callingClass); } return url; } }