简体   繁体   English

Java如何选择.JAR库版本

[英]How does java choose a .JAR library version

For various historical reasons, I have an old java application which shares a local lib/ directory of library .JARs with some other applications. 由于各种历史原因,我有一个旧的Java应用程序,它与其他一些应用程序共享库.JARs的本地lib/目录。 In this directory is a number of versioned copies of the same library, for example: 该目录中包含相同库的多个版本副本,例如:

...
log4j-1.2.16.jar
log4j-1.2.17.jar
slf4j-api-1.7.5.jar
slf4j-api-1.7.21.jar
slf4j-log4j12-1.7.5.jar
slf4j-log4j12-1.7.9.jar
...

When my java app starts, how does it decide which .jar file to load? 当我的Java应用启动时,它如何确定要加载哪个.jar文件? As far as I know, the CLASSPATH is just set to ./lib/ . 据我所知,CLASSPATH只是设置为./lib/ But it uses the Tanuki Service wrapper to start, so I'm not 100% sure of this. 但是它使用Tanuki Service包装器启动,因此我对此不是100%肯定。

I can't delete any of these existing .JARs, is there a way to specify exactly which .JARs my app will use ? 我无法删除所有这些现有的.JAR,有没有一种方法可以确切指定我的应用将使用哪些.JAR?

After this old application was patched, now I'm getting a NoClassDefFoundError, and I suspect the app is loading an older (or newer) version of a .JAR that conflicts with another library (BoneCP and slf4j). 修补此旧应用程序后,现在我收到一个NoClassDefFoundError,我怀疑该应用程序正在加载与另一个库(BoneCP和slf4j)冲突的.JAR的旧(或更新)版本。

Which jar first in classpath that is picked up when JVM classloader is looking for a class. JVM classloader在寻找类时,首先在类路径中找到哪个jar。 So you can try to add those jars in different order to check which one breaks your application 因此,您可以尝试以不同的顺序添加这些罐子,以检查哪个罐子破坏了您的应用程序

If you are going to share lib directories like that, then each application needs to list the JAR files that it uses explicitly on the classpath. 如果要共享这样的lib目录,则每个应用程序都需要列出它在类路径上显式使用的JAR文件。 If you use a wildcard classpath entry (eg "lib/*"), then it is not specified which versions of the JARs will be used. 如果使用通配符类路径条目(例如“ lib / *”),则未指定将使用哪个版本的JAR。 The manual entry states: 手册条目指出:

Class Path Wild Cards 类路径通配符

Class path entries can contain the base name wildcard character ( ), which is considered equivalent to specifying a list of all of the files in the directory with the extension .jar or .JAR. 类路径条目可以包含基本名称通配符( ),它被认为等效于指定扩展名为.jar或.JAR的目录中所有文件的列表。 For example, the class path entry mydir/ specifies all JAR files in the directory named mydir. 例如,类路径条目mydir /指定目录mydir中的所有JAR文件。 A class path entry consisting of * expands to a list of all the jar files in the current directory. 由*组成的类路径条目扩展为当前目录中所有jar文件的列表。 Files are considered regardless of whether they are hidden (have names beginning with '.'). 无论文件是否被隐藏(文件名以“。”开头)都将被视为文件。

.... ....

The order in which the JAR files in a directory are enumerated in the expanded class path is not specified and may vary from platform to platform and even from moment to moment on the same machine. 在扩展的类路径中枚举目录中JAR文件的顺序未指定,并且可能因平台而异,甚至在同一台计算机上有时也有所不同。 A well-constructed application should not depend upon any particular order. 结构良好的应用程序不应依赖于任何特定顺序。 If a specific order is required, then the JAR files can be enumerated explicitly in the class path. 如果需要特定顺序,则可以在类路径中显式枚举JAR文件。

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

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