71

I have a folder in my C: drive as C:\app_config\java_app This folder contains some locale specific property files.

I have a java class (PrjPropertilesLocator) that loads the property files based on default locale on the startup of the web App.My web application is running inside tomcat. The problem is how should i set this directory C:\app_config\java_app in the tomcat classpath so that this becomes available to the ResourceBundle inside the PrjPropertilesLocator class. Is there a way i can set this folder specifically for a Single web app that needs it.I do not want to put the property files inside WEB-INF/classes folder.

On weblogic this runs fine.I set the directory inside the weblogic classpath in one of its startup scripts and it works fine. But on Tomcat i tried putting it in startup.bat also in setclasspath.bat, but was not able to do so successfully.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Rajat
  • 1,065
  • 1
  • 13
  • 20
  • 1
    Just read on this link http://www.chemaxon.com/jchem/doc/admin/tomcat.html, that tomcat doesnot use system classpath.I copied all the files from C:/app_config/java_app to /lib folder and it worked.Looks like tomcat doestnot use system classpath.Can we set the folder C:\app_config\java_app inside one of tomcats startup files. – Rajat Aug 19 '09 at 16:34
  • 2
    http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html – jack Nov 06 '10 at 16:07

6 Answers6

127

Just specify it in shared.loader or common.loader property of /conf/catalina.properties.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • 13
    I wonder if there's a way to specify any of these using command-line parameters. It'd be very handy as I'd not have to modify any Tomcat files for this to work... – Matthias Hryniszak Feb 11 '12 at 19:59
  • 4
    I have spent hours looking for this solution. Once again BalusC has come through. – Rhys Feb 13 '13 at 06:33
  • 1
    People might be interested in [Understanding The Tomcat Classpath - Common Problems And How To Fix Them](https://www.mulesoft.com/tcat/tomcat-classpath) as a great relevant article. – Dawngerpony May 06 '15 at 14:00
  • 8
    Answer doesn't address key requirement: "Is there a way i can set this folder specifically for a Single web app that needs it." – Derek Mahar Jul 23 '15 at 01:07
  • 2
    This isn't a solution. Two applications may use the same config file name, so applications must have their own unique classpaths to be safe. – jacekn Mar 14 '16 at 03:12
  • 1
    @jacekn: OP asked how to add directory to Tomcat's classpath, not how to add directory to webapp's classpath. – BalusC Mar 14 '16 at 06:10
  • I used common.loader in /conf/catalina.properties and my properties file is loaded into class path. No issues but I see that the entry that I make gets autodeleted after some time. – Noor Syed Apr 22 '16 at 19:08
  • If you are raising your virtual machine with a vagrant, maybe you don't like to edit the catalina.properties file from the provisioner. You can symlink your classes into some of the already-listed directories in the catalina.properties. For example, I did this `ln -sf /vagrant/build/xavi_montero /var/lib/tomcat8/lib/` in my provisioner and it solved the problem. – Xavi Montero Aug 01 '16 at 14:32
  • Indeed I too want a command line switch to append to the classpath. What good is modifying catalina.properties when I'm trying to deploy via a chef recipe?! – Sridhar Sarnobat Nov 22 '17 at 18:58
  • shared.loader looks like deprecated, at least it was removed from documentation. But common.loader is visible for Tomcat itself which may be not safe to use. – Sergey Ponomarev Jun 15 '18 at 09:02
  • typing plus one so that the receiver should know, despite the fact that SO says not to do so :) – irshad.ahmad Mar 19 '19 at 09:10
29

See also question: Can I create a custom classpath on a per application basis in Tomcat

Tomcat 7 Context hold Loader element. According to docs deployment descriptor (what in <Context> tag) can be placed in:

  • $CATALINA_BASE/conf/server.xml - bad - require server restarts in order to reread config
  • $CATALINA_BASE/conf/context.xml - bad - shared across all applications
  • $CATALINA_BASE/work/$APP.war:/META-INF/context.xml - bad - require repackaging in order to change config
  • $CATALINA_BASE/work/[enginename]/[hostname]/$APP/META-INF/context.xml - nice, but see last option!!
  • $CATALINA_BASE/webapps/$APP/META-INF/context.xml - nice, but see last option!!
  • $CATALINA_BASE/conf/[enginename]/[hostname]/$APP.xml - best - completely out of application and automatically scanned for changes!!!

Here my config which demonstrate how to use development version of project files out of $CATALINA_BASE hierarchy (note that I place this file into src/test/resources dir and intruct Maven to preprocess ${basedir} placeholders through pom.xml <filtering>true</filtering> so after build in new environment I copy it to $CATALINA_BASE/conf/Catalina/localhost/$APP.xml):

<Context docBase="${basedir}/src/main/webapp"
         reloadable="true">
    <!-- http://tomcat.apache.org/tomcat-7.0-doc/config/context.html -->
    <Resources className="org.apache.naming.resources.VirtualDirContext"
               extraResourcePaths="/WEB-INF/classes=${basedir}/target/classes,/WEB-INF/lib=${basedir}/target/${project.build.finalName}/WEB-INF/lib"/>
    <Loader className="org.apache.catalina.loader.VirtualWebappLoader"
            virtualClasspath="${basedir}/target/classes;${basedir}/target/${project.build.finalName}/WEB-INF/lib"/>
    <JarScanner scanAllDirectories="true"/>

    <!-- Use development version of JS/CSS files. -->
    <Parameter name="min" value="dev"/>
    <Environment name="app.devel.ldap" value="USER" type="java.lang.String" override="true"/>
    <Environment name="app.devel.permitAll" value="true" type="java.lang.String" override="true"/>
</Context>

UPDATE Tomcat 8 change syntax for <Resources> and <Loader> elements, corresponding part now look like:

<Resources>
    <PostResources className="org.apache.catalina.webresources.DirResourceSet"
                   webAppMount="/WEB-INF/classes" base="${basedir}/target/classes" />
    <PostResources className="org.apache.catalina.webresources.DirResourceSet"
                   webAppMount="/WEB-INF/lib" base="${basedir}/target/${project.build.finalName}/WEB-INF/lib" />
</Resources>
Community
  • 1
  • 1
gavenkoa
  • 37,355
  • 13
  • 206
  • 248
  • Does anyone have an example of this working? We've been playing around with it for a while but haven't been able to properly add things to the classpath for the app to see. – Skeeterdrums May 09 '19 at 12:53
13

In Tomcat 6, the CLASSPATH in your environment is ignored. In setclasspath.bat you'll see

set CLASSPATH=%JAVA_HOME%\lib\tools.jar

then in catalina.bat, it's used like so

%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% 
-Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" 
-Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" 
-Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%

I don't see any other vars that are included, so I think you're stuck with editing setclasspath.bat and changing how CLASSPATH is built. For Tomcat 6.0.20, this change was on like 74 of setclasspath.bat

set CLASSPATH=C:\app_config\java_app;%JAVA_HOME%\lib\tools.jar
Trenton
  • 10,692
  • 9
  • 50
  • 57
5

What I suggest you do is add a META-INF directory with a MANIFEST.MF file in .war file.

Please note that according to servlet spec, it must be a .war file and not .war directory for the META-INF/MANIFEST.MF to be read by container.

Edit the MANIFEST.MF Class-Path property to C:\app_config\java_app:

See Using JAR Files: The Basics (Understanding the Manifest)

Enjoy.

ug_
  • 10,236
  • 2
  • 32
  • 49
Koekiebox
  • 5,305
  • 12
  • 50
  • 86
  • Well, it's not working for me. Note the sources put: You may need to reference classes in other JAR files from within a JAR file. The way may works for `jar`, but not for my web application. – WesternGun Mar 08 '16 at 12:57
2

You can create a new file, setenv.sh (or setenv.bat) inside tomcats bin directory and add following line there

export CLASSPATH=$CLASSPATH:/XX/xx/PATH_TO_DIR
Sudhir N
  • 3,890
  • 1
  • 20
  • 31
0

I might be a bit late for the party but I follow below steps to make it fully configurable using IntelliJ's way of in-IDE app test. I believe the best way to go with is to Combine below with @BelusC's answer.

1. run the application using IDE's tomcat run config. 
2. ps -ef | grep -i tomcat //this will give you a good idea about what the ide doing actually.
3. Copy the -Dcatalina.base parameter value from the command. this is your application specific catalina base. In this folder you can play with catalina.properties, application root path etc.. basically everything you have been doing is doable here too. 
Olgun Kaya
  • 2,282
  • 4
  • 26
  • 41