简体   繁体   中英

tomcat 8, how to access external java jar library files

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>

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

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

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" ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM