在应用程序中使用多个版本的 jar 会导致问题吗?
我遇到了一个包含多个版本的 jar 文件的应用程序。例如commons-fileupload-1.8.jar和commons-fileupload-1.6.jar。
这会导致任何问题吗?
谢谢, 拉古拉姆
I came across an application in which multiple versions of jar files are included. For instance commons-fileupload-1.8.jar and commons-fileupload-1.6.jar.
Would this cause any issues?
Thanks,
Raghuram
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,这是一个坏主意。如果幸运的话,可能发生的情况是,类路径中最先出现的两个版本中的任何一个都将满足所有引用。如果发生这种情况,那么 .jar 文件的其他版本根本不重要。然而,依赖于旧版本库的旧代码可能会错误地选择某些类的新版本,因此可能会发生各种奇怪的坏事情。
现在,在具有许多单独的类加载器的应用程序中,只要具有单独的类加载器的单独子系统将不同版本分开,这样的事情就可能成功。但是,如果您讨论的是系统类路径中对 .jar 的多个引用,那么这不是多个类加载器的情况。
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. 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 将是第一个加载的 jar,它基于类加载器,但我认为不是按保证的顺序加载的。因此,这意味着某些代码可能依赖于 1.8 版本中的某个功能,然后 1.6 版本会被加载,并在您尝试使用它时引发异常。
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. 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.
只有当两个版本实际上都是通过同一个类加载器加载时才会出现问题,例如两者都出现在常规类路径上。
如果通过单独的类加载器加载不同的版本,它就可以工作。
想必您所查看的应用程序正在执行此操作。或者他们只是升级了 JAR 而忘记删除旧版本。
There will only be issues if both versions are actually loaded through the same class loader, e.g. 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 中都存在的 X 类,则 X.class 其中一个将由类加载器加载,并且假设需要两个 jar 中的类 Y ,那么其中一个将被加载(通常第一个)但不能保证它们来自同一个罐子。
因此,如果同一个 jar 有两个版本,您需要检查为什么会发生这种情况,并尝试删除其中一个。 (如果您使用 Maven,则有不同的方法可以实现此目的)
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.
So if there are two versions of same jar you need to inspect why this is happening and try and remove one of them. (If you are using maven there are different ways of achieving this)
是的,它会导致问题,因为实际上只会使用其中之一,具体取决于类加载器加载哪一个以及它们加载的顺序。
yes it causes problems because only one of them will actually be used depending on which one gets loaded by the class loader(s) and what order they are loaded.