Tomcat 8 classloading - difference between JAR in [WEB-INF/lib] and [tomcat/lib]

前端 未结 2 699
悲&欢浪女
悲&欢浪女 2021-01-19 11:05

It says here that the common classloader is visible to all web application. So what is the difference between having a JAR file within the WEB-INF/lib folder of

相关标签:
2条回答
  • 2021-01-19 11:19

    The tomcat/lib directory should be used for shared libraries for the whole web server and WEB-INF/lib only for one web application.

    0 讨论(0)
  • 2021-01-19 11:27

    The difference is the classloader being used. Tomcat has very specific rules for it's classloaders, some of them are counter intuitive. Your problem here, is that a class loaded in one classloader is not compatible with a class loaded in another. Even Object to Object will fail.

    The jar containing your interface is loaded by the webapp classloader since it is located in the war-file. The implementation is loaded by the common classloader. This does not contain your interface, so it throws a ClassNotFoundException.

    The solution is to move all jars into the same classloader. Either everything in the lib-folder, or everything in your war.

    Implementing a plug-in architecture with Tomcat is a rather difficult undertaking. Using something like OSGi would probably help. But for small problems, it's probably overkill. Fun, but overkill.

    0 讨论(0)
提交回复
热议问题