Java Classloader - how to reference different versions of a jar

前端 未结 5 1155
一个人的身影
一个人的身影 2020-12-03 07:28

This is a common problem. I\'m using 2 libraries A.jar and B.jar and these depend on different versions of the same jar.
Let\'s say tha

5条回答
  •  暖寄归人
    2020-12-03 07:57

    "Old way" you mentioned (and the one OSGI certainly uses under the hood) is to install your own ClassLoader for both branches of your dependencies. That's how, for instance, application servers are able to run both older and newer versions of the same application inside the same JVM.

    Read about classloader hierarchy.

    In your setup, the tricky part is the joint point, where classes from both branches meet. Neither branches can use classes loaded into another one. The way to make it work is to make sure only classes loaded by boot classloader (JRE classes) or classloader of MY.jar are passed down to both branches.

提交回复
热议问题