java 控制 Tomcat 5 上 WEB-INF/lib 中 jars 的类路径顺序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2021227/
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
Control the classpath ordering of jars in WEB-INF/lib on Tomcat 5?
提问by Robert Campbell
I have a legacy web app running in Tomcat 5.0.
我有一个在 Tomcat 5.0 中运行的旧版 Web 应用程序。
This web app has two jars in WEB-INF/lib, let's say Foo-2.0.jarand Bar-2.0.jar. Bar-2.0.jaractually includes a Foo-1.0.jarinside of it. Bar is also a dead project, meaning no upgrading, no source, but still important to the application.
这个 Web 应用程序在WEB-INF/lib 中有两个 jar,比如说Foo-2.0.jar和Bar-2.0.jar。Bar-2.0.jar实际上在其中包含了一个Foo-1.0.jar。Bar 也是一个死项目,意味着没有升级,没有源,但对应用程序仍然很重要。
The latest release of this application requires Foo-2.0.jarfor some other stuff. Having both Foo-1.0.jarand Foo-2.0.jarin the classpath creates a conflict, specifically a ClassDefNotFoundtype of error, where a class that was later added in 2.0 cannot be found in 1.0, etc.
这个应用程序的最新版本需要Foo-2.0.jar来处理其他一些东西。在类路径中同时包含Foo-1.0.jar和Foo-2.0.jar会产生冲突,特别是ClassDefNotFound类型的错误,即后来在 2.0 中添加的类在 1.0 中找不到,等等。
In Eclipse, the simple solution is to right click on your Project, click Properties> Java Built Path> Order and Exportand to move Foo-2.0.jarabove Bar-2.0.jarso it's resolved first.
在 Eclipse 中,简单的解决方案是右键单击您的Project,单击Properties> Java Built Path> Order and Export并将Foo-2.0.jar移动到Bar-2.0.jar上方,以便首先解决。
How does one accomplish this type of classpath ordering for jars in WEB-INF/libin Tomcat?
如何在 Tomcat 的WEB-INF/lib中完成这种类型的 jar 类路径排序?
采纳答案by Georgy Bolyuba
Put Foo-1.0.jar to $CATALINE_HOME/common/endorsed (or any other place where it will be loaded after Foo-2.0.jar).
将 Foo-1.0.jar 放到 $CATALINE_HOME/common/endorsed(或在 Foo-2.0.jar 之后加载的任何其他地方)。
回答by BalusC
Tomcat 5's classloading precedencefor webapps is roughly as follows: first the bootstrap/system (JRE/lib, then Tomcat's internal classes), then the webapp libraries (first WEB-INF/classes, then WEB-INF/lib), then the common libraries (first Tomcat/common, then Tomcat/lib) and finally the webapp-shared libraries (Tomcat/shared).
Tomcat 5对 webapps的类加载优先级大致如下:首先是引导程序/系统(JRE/lib,然后是 Tomcat 的内部类),然后是 webapp 库(首先WEB-INF/classes,然后是WEB-INF/lib),然后是公共库(首先Tomcat/common,然后是Tomcat/lib),最后是 webapp 共享库( Tomcat/shared).
So to get Foo-2.0.jarloaded beforeBar-2.0.jar, best what you can do is to move Bar-2.0.jarfrom WEB-INF/libto Tomcat/commonor Tomcat/shared.
因此,要在之前Foo-2.0.jar加载,最好的方法是从to或移动。Bar-2.0.jarBar-2.0.jarWEB-INF/libTomcat/commonTomcat/shared
The JAR's aren't loaded in alphabetic order of their name. At least, there's no spec which says that. Renaming them to change the alphabetical filename order makes no sense.
JAR 不是按其名称的字母顺序加载的。至少,没有规范说明这一点。重命名它们以更改按字母顺序排列的文件名顺序是没有意义的。
回答by David Soroko
Strip Foo-1.0.jarout of Bar-2.0.jar. As it is, it's just trouble waiting to happen both for development(need to fudge the dev. environments) and for deployment.
从Bar-2.0.jar 中去除Foo-1.0.jar。事实上,等待开发(需要伪造开发环境)和部署都是麻烦的。
回答by jsight
You don't, as this feature isn't available in Tomcat. If both Foo-1.0.jar and Foo-2.0.jar are needed in the classpath at the same time, you will need some major classpath reorganization.
您没有,因为此功能在 Tomcat 中不可用。如果在类路径中同时需要 Foo-1.0.jar 和 Foo-2.0.jar,您将需要一些主要的类路径重组。
If Bar-2.0 can work with Foo-2.0, then the best thing to do would be to rebuild Bar-2.0 yourself without a Foo-1.0.jar inside of it.
如果 Bar-2.0 可以与 Foo-2.0 一起工作,那么最好的办法就是自己重建 Bar-2.0,而不要在里面安装 Foo-1.0.jar。
回答by Kennet
It is possible to set the Class-Path in the mainfest of the jar. http://java.sun.com/developer/Books/javaprogramming/JAR/basics/manifest.htmlI can't really promise it will solve the problem, but can be worth a try.
可以在 jar 的 mainfest 中设置 Class-Path。http://java.sun.com/developer/Books/javaprogramming/JAR/basics/manifest.html我不能保证它会解决问题,但值得一试。
回答by weberjn
To have Foo-2.0.jar before Bar-2.0.jar, update the Bar-2.0.jar with the content of Foo-2.0.jar (overwrite already contained .class)and delete Foo-2.0.jar from the war.
要在 Bar-2.0.jar 之前拥有 Foo-2.0.jar,请使用 Foo-2.0.jar 的内容更新 Bar-2.0.jar(覆盖已包含的 .class)并从 war 中删除 Foo-2.0.jar。
-cp A.jar:B.jar has the effect, that content of A.jar is like a layer over B.jar. So you get the same effect with overwriting B.jar's content with A.jar's.
-cp A.jar:B.jar 的作用是,A.jar 的内容就像是 B.jar 上的一层。因此,用 A.jar 覆盖 B.jar 的内容可以获得相同的效果。
回答by Vinodh Ramasubramanian
This is a bit hacky but might work. Change the name of Foo-2.0.jar to be alphabetically ahead of Bar-2.0.jar, say AFoo-2.0.jar.
这有点hacky,但可能会奏效。将 Foo-2.0.jar 的名称更改为按字母顺序排列在 Bar-2.0.jar 之前,例如 AFoo-2.0.jar。

