简体   繁体   English

在应用程序中使用多个版本的jar会导致问题吗?

[英]Will using multiple version of a jar in an application cause problems?

I came across an application in which multiple versions of jar files are included. 我遇到了一个应用程序,其中包含多个版本的jar文件。 For instance commons-fileupload-1.8.jar and commons-fileupload-1.6.jar. 例如commons-fileupload-1.8.jar和commons-fileupload-1.6.jar。

Would this cause any issues? 这会引起任何问题吗?

Thanks, Raghuram 谢谢,Raghuram

Yes, that's a bad idea. 是的,这是一个坏主意。 What will probably happen if you're lucky is that whichever of the two versions that comes first in the classpath will satisfy all the references. 如果幸运的话可能会发生的情况是,类路径中首先出现的两个版本中的任何一个都将满足所有引用。 If that happens, then the other versions of the .jar file won't matter at all. 如果发生这种情况,那么.jar文件的其他版本根本不重要。 However, old code that relies on an old version of the library might incorrectly pick up new versions of some classes, and so all sorts of weird bad things can happen. 但是,依赖于旧版本库的旧代码可能会错误地获取某些类的新版本,因此会发生各种奇怪的错误。

Now, in an application with many separate class loaders, such a thing might work out, as long as the separate subsystems with separate class loaders keep the different versions separated. 现在,在具有许多单独的类加载器的应用程序中,只要具有单独的类加载器的单独子系统保持不同的版本分离,这样的事情可能会成功。 If you're talking about multiple references to a .jar in the system classpath, however, then it's not a case of multiple class loaders. 但是,如果您正在讨论系统类路径中对.jar的多个引用,那么它不是多个类加载器的情况。

In my experience, yes it will. 根据我的经验,是的。 The jar that gets used will be the one that is loaded first and that is based on the class loader and not, I think, in a guaranteed order. 使用的jar将是首先加载的jar,它基于类加载器,而不是,我认为,保证顺序。 So that means that some code might be depending on a feature in version 1.8 and then 1.6 gets loaded and throws an exception when you try to use it. 这意味着某些代码可能依赖于1.8版本中的功能,然后1.6加载并在您尝试使用时抛出异常。

There will only be issues if both versions are actually loaded through the same class loader, eg by both appearing on the regular classpath. 如果两个版本实际上都通过相同的类加载器加载,那么只会出现问题,例如,两者都出现在常规类路径中。

It can be made to work if you load the different versions through separate class loaders. 如果您通过单独的类加载器加载不同的版本,它可以工作。 Presumably the application you looked at is doing this. 据推测,您看到的应用程序正在执行此操作。 Or they just upgraded the JAR and forgot to delete the old version. 或者他们刚刚升级了JAR而忘记删除旧版本。

Definitely and it might give you different results sometimes depending on the app server and sometimes depending on the packaging. 当然,它可能会给你不同的结果,具体取决于应用服务器,有时取决于包装。

If your application uses a class say X which is in both jars, the X.class one of them will be loaded by the classloader, and lets say that needs a class Y which is in both jars again one of them will be loaded (usually the first) but there is no guarantee that they will be from same jar. 如果你的应用程序在两个jar中都使用了一个类说X,那么其中一个类将由类加载器加载,并且假设需要一个类Y在两个jar中再次加载其中一个(通常第一个)但不能保证它们会来自同一个罐子。

So if there are two versions of same jar you need to inspect why this is happening and try and remove one of them. 因此,如果同一个jar有两个版本,你需要检查为什么会这样,并尝试删除其中一个。 (If you are using maven there are different ways of achieving this) (如果你正在使用maven,有不同的方法来实现这一目标)

是的,它会导致问题,因为实际上只会使用其中一个,具体取决于哪个类加载器加载它们以及加载它们的顺序。

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

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