简体   繁体   English

tomcat 8,如何访问外部java jar库文件

[英]tomcat 8, how to access external java jar library files

I am using tomcat 8 and have an external library directory called /opt/thirdParty. 我正在使用tomcat 8并且有一个名为/ opt / thirdParty的外部库目录。 I have the third party library in this directory. 我在这个目录中有第三方库。 (ie. the library directory is outside of tomcat server) (即库目录在tomcat服务器之外)

and have context.xml under META-INF configured like this 并在META-INF下配置context.xml,如下所示

<?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 当我启动tomcat服务器并在catalina.out文件中获取错误

java.lang.ClassNotFoundException: externalPackage.ExteranlJavaClass 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. 我需要有关如何配置tomcat服务器的帮助,以便我可以访问/ opt / thirdParty目录中的外部库文件。

Thanks. 谢谢。

I also tried following and get the same error message: java.lang.ClassNotFoundException: externalPackage.ExteranlJavaClass thanks for help 我也试过跟随并得到相同的错误消息:java.lang.ClassNotFoundException:externalPackage.ExteranlJavaClass感谢您的帮助

<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 通过将外部库移动到应用程序服务器的common-libs / share-libs目录,将外部库添加到服务器类路径
  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 如果使用webapp规范> 3.0,您可以将您的库作为Web片段并将其部署到app-server

Can't you just make a symlink from /opt/3rdParty/lib1.jar to CATALINA_BASE/lib folder? 你能不能从/opt/3rdParty/lib1.jarCATALINA_BASE/lib文件夹创建一个符号链接?

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. PreResources元素中的className属性指定 org.apache.catalina.WebResourceSet的实现,该实现用于加载资源。 Since you want to load a jar you can use the standard JarResourceSet 由于您要加载jar,因此可以使用标准JarResourceSet

<PreResources className="org.apache.catalina.webresources.JarResourceSet" ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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