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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21165208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:03:34  来源:igfitidea点击:

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

javatomcat

提问by user464592

I am developing web application using Tomcat 7. It uses my MyLib.jar that is placed under webapps\MyApplication\WEB-INF\lib. This library is successfully loaded by Tomcat. The problem is with libraries that are needed by MyLib.jar (let's say A.jar and B.jar).

我正在使用 Tomcat 7 开发 Web 应用程序。它使用位于 webapps\MyApplication\WEB-INF\lib 下的 MyLib.jar。这个库被Tomcat成功加载了。问题在于 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 时,我添加了带有 Class-path 的 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. I just don't want to put them there to keep Tomcat installation clean.

当我将 A.jar 和 B.jar 复制到 \lib 目录时,这一切都对我有用。我只是不想把它们放在那里以保持 Tomcat 安装干净。

Maybe I need to specify extra class path for MyApplication? Maybe globally for Tomcat? How then? Please provide any suggestions.

也许我需要为 MyApplication 指定额外的类路径?也许对于 Tomcat 来说是全球性的?那么如何?请提供任何建议。

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. It works for me, but could you tell me why it is not working with "otherLibs" directory?

编辑:奇怪。我运行了一些额外的测试。我将 MyLib.jar 的类路径更改为“A.jar B.jar”(没有 otherLibs 目录),将 A.jar 和 B.jar 放在 MyLib.jar 旁边,现在它可以正常工作了。它对我有用,但是您能告诉我为什么它不适用于“otherLibs”目录吗?

采纳答案by Santosh

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, i.e. Bootstrap classloader -> Extension classloader -> Application classloader. This application classloaderloads the classes from CLASSPATHenvironment variable, -classpathor -cpcommand line option, Class-Pathattribute of Manifestfile inside JAR. This is the reason why it works from command line. More here.

  2. The Server(like tomcat) classloading mechanism is differentfrom the standalone application. They use custom class loading. This is the reason that only putting your all jars in WEB-INF/libsimply works as the custom class loader is meant to load jars from WEB-INF/libfolder in your application. As is evident, the custom classloader does not regard the entries in Manifestfile inside JAR and hence ignores all the jars not directly in WEB-INF/lib. It works when you copy the jars in WEB-INF/libas custom class loader is not able to find them.

  1. 当您从命令行运行 jar 时,JVM 使用标准的类加载机制,即Bootstrap 类加载器 -> 扩展类加载器 -> 应用类加载器。此应用程序类加载CLASSPATH环境变量-classpath-cp命令行选项、JAR 文件的Class-Path属性加载类Manifest。这就是它从命令行工作的原因。更多在这里

  2. 服务器如Tomcat的类加载机制是不同的,从独立的应用程序。他们使用自定义类加载。这就是仅将所有 jar 放入的原因,WEB-INF/lib因为自定义类加载器旨在从WEB-INF/lib应用程序中的文件夹加载 jar 。很明显,自定义类加载器不会考虑ManifestJAR 文件中的条目,因此会忽略所有不直接在 .jar 文件中的 jar 文件WEB-INF/lib。当您复制 jar 时,它会起作用,WEB-INF/lib因为自定义类加载器无法找到它们。