Search code examples
javatomcat7classloader

interface implemetation in tomcat lib folder not able to refer to interface present in web-inf/classes folder


I have a interface defined in a war file under WEB-INF/classes folder. The idea is to have an implementation to be placed in class path without disturbing the war file. The solution I thought was to place the implementation in tomcat_home/lib directory but due to possible use of different classloader for implementation and interface, the interface is not getting resolved and ClassNotFound is being thrown. Is this scenario possible to achieve?


Solution

  • You are getting this error because the your implementation is loading in memory before the interface because Class loaders in the Application Server runtime follow a delegation hierarchy where classes in tomcat lib is loaded before classes in web-inf/classes. So if you want to remove the error keep both in tomcat lib, so that both will load at same time.

    enter image description here

    In the delegation design, a class loader delegates classloading to its parent before attempting to load a class itself. A class loader parent can be either the System class loader or another custom class loader. If the parent class loader cannot load a class, the class loader attempts to load the class itself. In effect, a class loader is responsible for loading only the classes not available to the parent. Classes loaded by a class loader higher in the hierarchy cannot refer to classes available lower in the hierarchy.