Search code examples
javaspringtomcatactivemq-classicclassloader

Need help understanding Tomcat vs WepApp classloading issue


I have 3 webapps running on Tomcat. They all use ActiveMQ. For that reason, i put activemq-all.jar in the Tomcat /lib folder.

With Spring in my webapp i have connectionfactories and listeners. They are created when my webapp boots, but classes they use are loaded from the jars in the lib folder.

The thing i think i see is that any dependency that activemq-classes have, must also reside within the tomcat lib. For example a thing such as xbean-spring.jar, or any Spring classes. Even if i have them in my war lib, i get ClassNotFound.

Why is that? I thought that the classloader for my webapp was a joint set of classes from my war, and classes that reside in the tomcat lib, but it seems that if the class resides in tomcat lib, it cannot "see" the classes in my war lib.

If anybody can help me get my head around this, i'd appreciate it.


Solution

  • Tomcat have several classloaders, you are putting jar in Common classloader

    Normally, application classes should NOT be placed here

    It can search classes only in itself or parent class loaders, which doesn't include your webapp

    In a Java environment, class loaders are arranged in a parent-child tree. Normally, when a class loader is asked to load a particular class or resource, it delegates the request to a parent class loader first, and then looks in its own repositories only if the parent class loader(s) cannot find the requested class or resource.

      Bootstrap
          |
       System
          |
       Common
       /     \
    Webapp1   Webapp2 ...