20

In Tomcat I want to use a jar inside a web application. The jar file will exist outside of the Tomcat directory.

To include the jar file in tomcat classpath, I modified the TomcatHome/conf/catalina.properties to include the absolute path of my jar file like,

shared.loader=D:\jaa\MyJarFile.jar

as per the suggestion given in link,

http://www.mulesoft.com/tomcat-classpath

But it throws the error,

java.lang.NoClassDefFoundError

I have also tried ,

shared.loader=D:\jaa\*.jar

shared.loader=file:\\D:\jaa\MyJarFile.jar

None of them seem to work :(

If I try placing the jar inside tomcat/lib it seem to work. But I am not allowed to do that.

Please help me out with this issue as I have implementation the next week..

Sathesh
  • 6,323
  • 6
  • 36
  • 48
  • Did you try: `file://path/to/foldername/jarname.jar`?Your syntax is different – Cratylus May 28 '11 at 19:40
  • Tomcat raises IllegalArgumentsException when I use forward slash(/) while server startup. So I got to use backslash(file:\\D:\jaa\PatMS.jar) – Sathesh May 29 '11 at 06:51
  • Possible duplicate of [How to add JAR libraries to WAR project without facing java.lang.ClassNotFoundException? Classpath vs Build Path vs /WEB-INF/lib](http://stackoverflow.com/questions/6792197/how-to-add-jar-libraries-to-war-project-without-facing-java-lang-classnotfoundex) –  Aug 18 '16 at 10:05
  • What worked for me was to use quotes and forward slashes, like _shared.folder="C:/Program Files/Java/jdk1.8.0_121/db/lib/*.jar"_ (on a Windows system). – D.Ogranos Feb 23 '17 at 08:42

3 Answers3

15

I figured myself how to add the classpath for tomcat. Instead of editing catalina.properties, just create a "setenv.sh" in the Tomcat Bin directory with the classpath,

Example,

CLASSPATH=D:\jaa\MyJarFile.jar

I just checked the catalina.sh in Tomcat/bin and these classpath variable will be set while setting the bootstrap as the classpath.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
Sathesh
  • 6,323
  • 6
  • 36
  • 48
  • 3
    It's worth noting that the `CLASSPATH` environment variable is explicitly *unset* by `catalina.sh`, so you must use edit `setenv.sh` to set it. – z0r Jun 12 '14 at 03:06
1

I was using IntelliJ and I tried everything like:

  1. Using CtrlAltShiftS (Project Settings) and adding a dependency of mysql-connector.jar. Didn't work. (The only thing that worked was that code completion inside IntelliJ was working fine.
  2. Adding mysql-connector.jar to apache-home/lib/ folder. Didn't work.
  3. Including mysql-connector.jar from Maven inside IntelliJ. Just didn't work.

The thing that worked for me:

Include the mysql-connector.jar file in the PROJECT/web/WEB-INF/lib folder.

No need to add it as a dependency anywhere. Just compile this and it will work fine.

Arpan Srivastava
  • 356
  • 5
  • 10
0

I find myself copying extra JARs, which should be available for all contexts and should therefor go into the root loader, into the following directory:

 C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib

But this only possible if you have access to this directory, which might not be the case for all ISPs. At least you can do it at home.

Bye

  • 2
    This makes the jar file available for all the web applications deployed in the server. The question mentions for a web application. – Sathesh Aug 17 '16 at 22:45
  • For the web application put it into WEB-INF/lib of your web application context. It will be automatically loaded by the context classloader. –  Aug 18 '16 at 10:03