The correct behaviour for a classloader in Java is to:
- If it has already been loaded, return the class
- Call the parent loadClass()
- Try and load the class itself.
So the class defined in the system classpath should always get loaded first. Tomcat defines classloader per war, which has the system classloader as a parent, so if you try to load a class, it will first look in the system classpath and then in the classpath defined in the war file.
As per my understanding, this is for two reasons:
- To avoid problems with different versions of classes being used. Imagine I redefined java.lang.Object in a war, it would be a nightmare.
- To avoid dependencies on child classloaders: The system classloader cannot depend on child classloaders: it would be hard to redeploy a war, for instance.
So, the question is:
In addition to the above problems, are there any other pitfalls to implementing a classloader which does not do a parent search first?