简体   繁体   English

Tomcat无法访问WEB-INF / lib中我的库引用的jar库

[英]Tomcat can't access jar library referenced by my library in WEB-INF/lib

I am developing web application using Tomcat 7. It uses my MyLib.jar that is placed under webapps\\MyApplication\\WEB-INF\\lib. 我正在使用Tomcat 7开发Web应用程序。它使用放在webapps \\ MyApplication \\ WEB-INF \\ lib下的MyLib.jar。 This library is successfully loaded by Tomcat. Tomcat已成功加载此库。 The problem is with libraries that are needed by MyLib.jar (let's say A.jar and B.jar). 问题在于MyLib.jar需要的库(比如A.jar和B.jar)。

While creating MyLib.jar I added MANIFEST with Class-path: otherLibs\\A.jar otherLibs\\B.jar (which are placed under webapps\\MyApplication\\WEB-INF\\lib\\otherLibs). 在创建MyLib.jar时,我添加了带有类路径的MANIFEST:otherLibs \\ A.jar otherLibs \\ B.jar(放在webapps \\ MyApplication \\ WEB-INF \\ lib \\ otherLibs下)。

What is interesting, MyLib.jar can be run from command line without any problems. 有趣的是,MyLib.jar可以从命令行运行而不会出现任何问题。

It all works for me when I copy A.jar and B.jar to \\lib directory. 当我将A.jar和B.jar复制到\\ lib目录时,这一切都适用于我。 I just don't want to put them there to keep Tomcat installation clean. 我只是不想把它们放在那里以保持Tomcat安装清洁。

Maybe I need to specify extra class path for MyApplication? 也许我需要为MyApplication指定额外的类路径? Maybe globally for Tomcat? 也许全球为Tomcat? How then? 那么如何? Please provide any suggestions. 请提供任何建议。

EDIT: Strange. 编辑:奇怪。 I run some additional tests. 我运行了一些额外的测试。 I changed classpath of MyLib.jar to "A.jar B.jar" (without otherLibs directory), put A.jar and B.jar next to MyLib.jar and now it works fine. 我将MyLib.jar的类路径更改为“A.jar B.jar”(没有otherLibs目录),将A.jar和B.jar放在MyLib.jar旁边,现在它工作正常。 It works for me, but could you tell me why it is not working with "otherLibs" directory? 它适用于我,但你能告诉我为什么它不使用“otherLibs”目录吗?

Here is whats going on with your application (trying to answer could you tell me why it is not working with "otherLibs" directory? ): 以下是您的应用程序的最新情况(尝试回答,您能告诉我为什么它不能与“otherLibs”目录一起工作吗? ):

  1. When you run your jar from the command line, JVM uses the standard class-loading mechanism, ie Bootstrap classloader -> Extension classloader -> Application classloader . 当您从命令行运行jar时,JVM使用标准的类加载机制,即Bootstrap类加载器 - >扩展类加载器 - >应用程序类加载器 This application classloader loads the classes from CLASSPATH environment variable, -classpath or -cp command line option, Class-Path attribute of Manifest file inside JAR. 应用程序类加载CLASSPATH环境变量, -classpath-cp命令行选项,JAR中的Manifest文件的Class-Path属性加载类。 This is the reason why it works from command line. 这就是它从命令行运行的原因。 More here . 更多这里

  2. The Server ( like tomcat ) classloading mechanism is different from the standalone application. Server如tomcat类加载机制与独立应用程序不同 They use custom class loading . 他们使用自定义类加载 This is the reason that only putting your all jars in WEB-INF/lib simply works as the custom class loader is meant to load jars from WEB-INF/lib folder in your application. 这就是为什么只将你的所有jar放在WEB-INF/lib因为自定义类加载器意味着从应用程序中的WEB-INF/lib文件夹加载jar。 As is evident, the custom classloader does not regard the entries in Manifest file inside JAR and hence ignores all the jars not directly in WEB-INF/lib . 很明显,自定义类加载器不考虑JAR中Manifest文件中的条目,因此忽略了不直接在WEB-INF/lib所有jar。 It works when you copy the jars in WEB-INF/lib as custom class loader is not able to find them. 当您在WEB-INF/lib复制jar时,它会起作用,因为自定义类加载器无法找到它们。

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

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