If one does not want to bloat up the application wars by including all possible library jars, the standard way is to share the jars by putting them in Tomcat’s common lib directory so that they can be handled by the common loader rather than the web-app loader. To use a separate directory and to avoid cluttering common lib directory, one can enable the shared loader in conf/catalina.properties. Another alternative is to include the class path in Tomcat’s start-up class-path.
However these approaches do not work when
- One wants to use different versions of the shared jars for different applications
- One does not want to share the jars with all web-apps hosted on the Tomcat instance
- Deployment/undeployment may leave the shared jars debris behind (similar to the problem of leaving shared dlls left behind in c:\windows\system32)
In such a case, we need to define an application specific custom class path.
org.apache.catalina.loader.VirtualWebappLoaderallows one to specify additional class paths. You specify the additional paths via the virtualClasspath attribute in your <Context> element.
The META-INF/context.xml for the application needs to be updated with an entry similar to the following, One can also specify searching the virtual class path first so that the classes in the virtual class path have precedence over the classes in the war.
<Context antiJARLocking=”true” path=”/mywebapp”>
Usage of this class is discouraged in the Tomcat documentation since including external directories might pose security problems.