/tomcat/lib
dir I have class SharedClass
(which is thus shared with all web apps).WEB-INF/lib
I have class LocalClass
. SharedClass
has a reference to LocalClass
.In my web app I try to create an instance of SharedClass
but it fails with the message:
NoClassDefFoundError: LocalClass.
Since SharedClass
is shared and LocalClass
is local to my web app I was hoping it would work, but it doesn't.
My suspicion is that SharedClass
is loaded by the Tomcat parent classloader and LocalClass
is loaded by the Web App classloader. Since SharedClass
was loaded by the parent, I assume that all of its dependencies must also be loaded by the parent. Thus, the parent can't find LocalClass
and it throws the Error.
Does this make sense? Is there any way around this (without writing my own classloader)?
ClassLoaders are hierarchical. A class loader has a parent, and sees the classes of its parent. The reverse is not true. So a class loaded by the webapp's classloader has access to the classes loaded by the common Tomcat classloader (its parent), which has access to the JRE classes (the parent of Tomcat's classloader).
See the Tomcat documentation and the ClassLoader javadoc for more details.