4

How do I add a directory to the classpath of the classloader assigned to a specific Tomcat 7 context?

I want my Java Servlet application to load a properties file external to the warfile from a directory in the classpath visible only to that servlet. For example, servlet context /appA on host localhost should load /etc/appA/application.properties (Linux) or D:\configuration\appA\application.properties (Windows) while servlet context /appB on the same host should not have /etc/appA (Linux) or D:\configuration\appA\ in its classpath.

Derek Mahar
  • 25,458
  • 37
  • 115
  • 164
  • 1
    I think [Can I create a custom classpath on a per application basis in Tomcat](http://stackoverflow.com/questions/6345793/can-i-create-a-custom-classpath-on-a-per-application-basis-in-tomcat) is a better choice as a duplicate than [Adding a directory to tomcat classpath](http://stackoverflow.com/questions/1300780/adding-a-directory-to-tomcat-classpath). – Derek Mahar Jul 23 '15 at 16:12

1 Answers1

3

If you want to add the path for all webbapps:

Add directory path name /etc/appA to common.loader property of /conf/catalina.properties file.

If you want to add the path to one specific webapp:

In file $CATALINA_BASE/conf/Catalina/localhost/appA.xml, inside element <Context>, use element <Loader> with class VirtualWebappLoader :

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/appA">
    <Loader className="org.apache.catalina.loader.VirtualWebappLoader"
              virtualClasspath="/etc/appA"/>
</Context>

References:

VirtualClassLoader

Derek Mahar
  • 25,458
  • 37
  • 115
  • 164
Ramesh PVK
  • 14,700
  • 2
  • 43
  • 49
  • Won't this path appear on the classpath of all contexts? I want the path to be visible to a particular context and that context only. – Derek Mahar Jul 22 '15 at 19:06
  • Your answer is correct, except the `` definition should appear in a file outside the application warfile in `$CATALINA_BASE/conf/Catalina/localhost/appA.xml` so that the path name prefix format may differ for different operating systems while using the same warfile. – Derek Mahar Jul 22 '15 at 21:14
  • Modified the answer to specify file `$CATALINA_BASE/conf/Catalina/localhost/appA.xml`. – Derek Mahar Jul 24 '15 at 13:38