Reverse Order Class Loader (Child First)   Leave a comment

Recently while working on using JMockit to write unit tests for some legacy code, I came across a problem with classloaders that forced me to write my own class loader. In Java, classloaders like URLClassLoader delegates to it’s parent classloader before searching it’s own classpath. The issue was that my JUnit tests were being launched in Eclipse by a laucher that created a new URLClassLoader and used that to launch the tests. This worked well in most cases except when using JMockit. The reason why this was a problem was because JMockit realized that it was being loaded by a custom class loader and tried to reinitialize itself with the URLClassLoader created by the launcher. This led to ClassNoDefFoundError where it couldn’t find the junit classes even though junit was present in the classpath. The reason I guess is that JMockit classes was found by the parent classloader of the URLClassLoader which was the system classloader that didn’t have the junit.jar in the classpath. Once a class is loaded by a classloader it uses that classloader to load every other class it needs. The solution for this was to use a Reverse Order ClassLoader or a class loader that searched it’s classpath first before delegating to it’s parent classloader. Here is the code of the ReverseOrderClassLoader:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Logger;

public class ReverseOrderClassLoader extends URLClassLoader {

    private static String classPath = "Very Very long classPath....";

    public ReverseOrderClassLoader(ClassLoader parent) {
    	System.out.println("Parent Class loader : " + parent.getClass().getName());        

    public static URL[] getClassPathAsURLs(String classpath) {
		 List urlList = new ArrayList();
       for(String path : classpath.split(";")){
           try {
               urlList.add(new File(path).toURI().toURL());
           } catch (MalformedURLException e) {
               // TODO Auto-generated catch block
       return urlList.toArray(new URL[]{});        

    protected synchronized Class<?> loadClass(String name, boolean resolve)
            throws ClassNotFoundException {
    	Logger logger = Logger.getLogger(ReverseOrderClassLoader.class.getName());"Received request to load class : " + name + " resolve = " + resolve);
        Class<?> c = findLoadedClass(name);
        if(c != null)"Found loaded class : " + name);

        if (c == null) {          
                try {
       "Trying to find class : " + name + " in local classpath");
                    c = findClass(name);
                } catch (ClassNotFoundException e) {
       "Class : " + name + " not found in local classpath. Delegating to parent classloader");
           "RtmCampaignPlacementAccess test.");
                	c = super.loadClass(name, resolve);
                    if(c == null)
           "Class : " + name + " not found in any classpath.");
                    else if(name.contains("RtmCampaignPlacementAccess"))
           "Found RtmCampaignPlacementAccess in classpath.");
        if (resolve) {
        return c;

    public URL getResource(String name) {    
    	Logger logger = Logger.getLogger(ReverseOrderClassLoader.class.getName());"Trying to find resource : " + name + " in local classpath.");
        URL url = findResource(name);
        if (url == null) {
"Couldn't find resource : " + name + " in local classpath. Delegating to parent");
            url = super.getResource(name);
            if(url == null)
   "Couldn't find resource : " + name + " in any of the paths returning null");
        return url;

    public Enumeration getResources(String name) throws IOException {

        Enumeration localUrls = findResources(name);
        printEnumerations("Local resources for : " + name, localUrls);

        Enumeration parentUrls = null;
        if (getParent() != null) 
            parentUrls = getParent().getResources(name);        
        printEnumerations("Parent resources for : " + name, parentUrls);

        final List urlList = new ArrayList();
        addURLsIntoList(urlList, localUrls);
        addURLsIntoList(urlList, parentUrls);

        return Collections.enumeration(urlList);

    public InputStream getResourceAsStream(String name) {
        URL url = getResource(name);
        try {
            return url != null ? url.openStream() : null;
        } catch (IOException e) {
        return null;

    private void printEnumerations(String header,Enumeration urlEnumerator){
    	Logger logger = Logger.getLogger(ReverseOrderClassLoader.class.getName());;
    	if(urlEnumerator != null){

    private void addURLsIntoList(List urlList,Enumeration urlEnumeration){
    	 if (urlEnumeration != null && urlList != null) {
             while (urlEnumeration.hasMoreElements()) {


Posted April 9, 2013 by salilsurendran in Uncategorized

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: