本文介绍了在 jvm 具有相同名称但版本不同的 jar 的情况下,类加载器如何工作,哪个将在运行时加载或两者都将加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个同名的 jars 正在应用程序中使用,但用于不同的目的.例如,jar 的名称是A1.jar".有不同的版本并在 if else 条件下使用.

There are two jars which have the same name which is being used in the application but for different purposes.The name of the jar for example is "A1.jar" having different versions and used in if else condition.

if this:
 then A1-10.2.3jar.create()
else:
 then A1-8.18.0jar.create()

反过来又具有不同的 create 方法实现.

which in turn have different implementations of create method.

问题是当应用加载时,类加载时会实例化哪个jar?或者根据条件加载相应的jar?我们如何确保每次都拿起正确的罐子.

The question is when the application is loaded which jar will be instantiated during class loading time? Or depending upon condition the corresponding jar will be loaded? How can we ensure the correct jar is picked up each time.

问题进一步转移到应用服务器场景,其中两个库都在 jboss/wildfly 上的 WEB-INF/lib 文件夹中.类加载器在那里的表现如何?

The question is further transported to application server scenario where both libs are in WEB-INF/lib folder on jboss/wildfly. How would classloader behave there?

这种情况发生在 wildfly 的一个案例中,其中具有相同名称的罐子,但它选择了错误的罐子并造成了问题.但同样的情况在另一个环境中运行良好......是否有任何顺序/优先级?

This happened in one of the cases for wildfly where had same name of jars but it was picking up the wrong one and creating an issue. But the same case was running fine on another environment..is there any order/precedence for this?

更新:

被调用时的类是不同的:假设 A2 和 A3 类,然后调用 A1 jar,它们是 A2 和 A3 类的依赖项,名称相同但版本不同.

The classes are different when it is being called:Say A2 and A3 class which then calls the A1 jar which are coming as a dependency from class A2 and A3 with same names but different versions.

所以,情况就是这样:

 if this:
     then A2.create()-> calls A1-10.2.3jar.respone()
    else:
     then A3.create()->calls A1-8.18.0jar.respone()

如果是这种情况,类加载器可以同时加载两个类还是随机加载?

If this is the case can classloader load both the classes or it can be random?

推荐答案

据我所知,jee 应用服务器在这种情况下必须使用哪个版本没有规范.因此,使用哪个 jar 是一种(确定性的)随机.

As far as i know, there is no specification for jee application server which version they have to use in this scenario. Therefore it's kind of (deterministic) random, which jar is used.

这确实应该避免,例如在 maven 文件中排除部署或 gradle 类似的东西.

This really should be avoided, for example with an exclude for the deployment in a maven file or something similar for gradle.

这篇关于在 jvm 具有相同名称但版本不同的 jar 的情况下,类加载器如何工作,哪个将在运行时加载或两者都将加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 20:04