Adding an external directory to Tomcat classpath
Asked Answered
M

6

75

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.

Madelon answered 19/8, 2009 at 15:36 Comment(2)
Just read on this link 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 <tomcat install>/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.Madelon
tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.htmlHeyward
D
135

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

Dynamism answered 7/12, 2009 at 23:32 Comment(11)
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...Meleager
I have spent hours looking for this solution. Once again BalusC has come through.Marinara
People might be interested in Understanding The Tomcat Classpath - Common Problems And How To Fix Them as a great relevant article.Amsden
Answer doesn't address key requirement: "Is there a way i can set this folder specifically for a Single web app that needs it."Pashm
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.Agnella
@jacekn: OP asked how to add directory to Tomcat's classpath, not how to add directory to webapp's classpath.Dynamism
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.Substituent
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.Anastigmat
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?!Cuspidation
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.Anthropologist
typing plus one so that the receiver should know, despite the fact that SO says not to do so :)Scylla
H
31

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>
Houseline answered 30/9, 2014 at 17:24 Comment(1)
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.Glycerinate
P
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
Putupon answered 19/8, 2009 at 17:31 Comment(0)
B
6

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.

Broody answered 25/8, 2009 at 9:8 Comment(1)
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.Heptarchy
E
3

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
Evanthe answered 16/3, 2016 at 18:0 Comment(0)
S
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. 
Streaming answered 6/2, 2020 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.