1

I am using tomcat 8 and have an external library directory called /opt/thirdParty. I have the third party library in this directory. (ie. the library directory is outside of tomcat server)

and have context.xml under META-INF configured like this

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resources className="externalPackage.ExteranlJavaClass">
        <PreResources className="externalPackage.ExteranlJavaClass"
            base="/opt/thirdParty/firstWebApp.jar"
            internalPath="/"
            webAppMount="/WEB-INF/lib" />
    </Resources>
</Context>

when I start the tomcat server and get error in the catalina.out file

java.lang.ClassNotFoundException: externalPackage.ExteranlJavaClass

I need help on how to configure the tomcat server so that I can access the external library files at /opt/thirdParty directory.

Thanks.

I also tried following and get the same error message: java.lang.ClassNotFoundException: externalPackage.ExteranlJavaClass thanks for help

<Context> <Loader className="externalPackage.ExteranlJavaClass" 
  virtualClasspath="/opt/thirdParty/firstWebApp.jar"/> <JarScanner     
  scanAllDirectories="true" /> 
</Context>
Steve
  • 256
  • 5
  • 12

3 Answers3

0

You have several options:

  1. Add your external directory to the server-classpath by changeing the server-startup settings in the start-script
  2. add external library to the server-classpath by moving it to common-libs/share-libs directory of your application server
  3. Load your class on runtime using an custom classloader
  4. move your external library to internal
  5. If using webapp specs > 3.0 you can make your library a web-fragment and deploy it to the app-server
NeoP5
  • 611
  • 7
  • 19
  • Thanks @NeoP5, I have to put the library directory outside of tomcat. do you have an example on "Add your external directory to the server-classpath by changeing the server-startup settings in the start-script"? – Steve Apr 15 '16 at 07:27
  • ok, so we kill option 2,4 and 5 -> keeps options 1 and 3 alive. Easiest method would be to use option 3 with a custom class-loader. see here: http://stackoverflow.com/questions/60764/how-should-i-load-jars-dynamically-at-runtime – NeoP5 Apr 15 '16 at 07:30
  • Here is an example how to add this to tomcat classpath; but be aware that this jar file is the present for all applications deployed on the server(!) and might create conflicts or strange behaviours if e.g. different implementation-versions are used on different apps: http://stackoverflow.com/questions/1300780/adding-a-directory-to-tomcat-classpath – NeoP5 Apr 15 '16 at 07:37
  • Thanks @NeoP5. yes, custom class-loader will work. The problem is that I need to know all the classes in the jar files. I have a lot of jar files and a lot of classes. I am wondering if there are simple ways, like configuration or add into class path? – Steve Apr 15 '16 at 07:38
  • If you did it for one jar the rest is easy. you write a classloader that can search in multiple jars in one directory OR you create a new URLClassLoader for each jar inside the directory by looping over files. once the class-loader is created all class-files inside the jar are accessible. (no need to do class for name for each single class ;) ) – NeoP5 Apr 15 '16 at 07:42
0

Can't you just make a symlink from /opt/3rdParty/lib1.jar to CATALINA_BASE/lib folder?

Note! Don't forget to fix file permission issues

WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52
0

The className attribute in the PreResources element specifies an implementation of org.apache.catalina.WebResourceSet which is used to load the resource. Since you want to load a jar you can use the standard JarResourceSet

<PreResources className="org.apache.catalina.webresources.JarResourceSet" ...
wero
  • 32,544
  • 3
  • 59
  • 84