简体   繁体   English

如何在运行时解决jar冲突?

[英]How to resolve jar conflicts in runtime?

I have two jars are conflicted. 我有两个罐子有冲突。 Both of them contain a class with the same package name and class name. 它们都包含一个具有相同包名和类名的类。 Due to same reasons, I cannot remove either of them. 由于同样的原因,我无法删除其中任何一个。

So, is there any way to resolve this issue? 那么,有什么方法可以解决这个问题吗? Ideally, I hope there is a way can let me decide the class in which jars should be invoked during runtime. 理想情况下,我希望有一种方法可以让我决定在运行时应该调用jar的类。

I do appreciate anyone's help. 我非常感谢任何人的帮助。

You can create a package structure (same as the one in your conflicting jars) in your project folder with a different name & copy respective contents from any of the jars. 您可以使用不同的名称在项目文件夹中创建包结构(与冲突的jar中的包结构相同),并从任何jar中复制相应的内容。 Use one package from the jar and the other from your project (the new package which you just created) I have done this very recently and it worked. 使用jar中的一个包和你项目中的另一个包(你刚刚创建的新包)我最近做了这个并且它有效。

Good Luck! 祝好运!

Your options in this case are. 在这种情况下你的选择是。

1.(Best way) Modify app code to use latest version of jar. 1.(最佳方式)修改应用程序代码以使用最新版本的jar。 If your program depends on another pluggin which depends on the outdated jar file check for latest version of the pluggin. 如果您的程序依赖于另一个依赖于过时jar文件的插件,请检查最新版本的插件。

  1. Use OSGi 使用OSGi

  2. Use two classloaders. 使用两个类加载器。 Need to study a little bit of theory and can sometimes lead to unexpected errors. 需要研究一点理论,有时可能会导致意外错误。

Now I am not going to put examples as there is plenty of info and examples in the links below to assist you 现在我不打算举例,因为下面的链接中有大量信息和示例可以帮助您

Java, Classpath, Classloading => Multiple Versions of the same jar/project Java,Classpath,Classloading =>同一个jar /项目的多个版本

Jar hell: how to use a classloader to replace one jar library version with another at runtime Jar hell:如何在运行时使用类加载器将一个jar库版本替换为另一个jar库版本

http://java.dzone.com/articles/java-classloader-handling http://java.dzone.com/articles/java-classloader-handling

http://javarevisited.blogspot.in/2012/12/how-classloader-works-in-java.html http://javarevisited.blogspot.in/2012/12/how-classloader-works-in-java.html

Anything that is dynamically linked will have to have some way to distinguish what to link with at run time. 任何动态链接的东西都必须有一些方法来区分运行时链接的内容。 That's the whole point of having package hierarchy, to compartmentalize the code. 这是包层次结构的全部要点,以划分代码。 You can get around the issue of identical class names but once the packages are the same I don't know of a way to force the JVM to link against one over another. 您可以解决相同类名的问题,但是一旦包相同,我就不知道强制JVM链接到另一个的方法。

You can use different classloaders and run conflicting components in different sandboxes. 您可以使用不同的类加载器并在不同的沙箱中运行冲突的组件。

There is no way to dynamically pick which jar to use if both jars are on the classpath, and you are using the system classloading mechanism. 如果两个jar都在类路径上,并且您正在使用系统类加载机制,则无法动态选择要使用的jar。 Java classloading picks you the first class file. Java类加载会选择第一个类文件。

This is old topic, but there was a solution for JAR conflicts, but it works only till JAVA 8. You could move your conflicting JARs into two separate folders, and then mention them in the correct order in "java.endorsed.dirs" property. 这是一个古老的主题,但有一个JAR冲突的解决方案,但它只适用于JAVA 8.您可以将冲突的JAR移动到两个单独的文件夹中,然后在“java.endorsed.dirs”属性中以正确的顺序提及它们。 This will force your app to pre-load the endorsed JARs and its classes in the specified order, forcing desired classes to be used later when called. 这将强制您的应用程序按指定的顺序预加载已批准的JAR及其类,从而强制在调用后使用所需的类。 I did it for my app which ran on JBoss and had 2 JARs with exactly same class and package. 我为我的应用程序做了这个,它在JBoss上运行并且有2个JAR,它们具有完全相同的类和包。 The complexity was that I ended up endorsing 23 JARs instead of 2, as there were dependencies, but finally the workaround worked - real issue was fixed. 复杂性是我最终支持23个JAR而不是2个,因为存在依赖关系,但最终解决方法有效 - 真正的问题已得到解决。 Unfortunately support of java.endorsed.dirs was removed starting from Java8. 不幸的是,从Java8开始删除了对java.endorsed.dirs的支持。 If anyone have an idea how to do similar trick for latest Java, please, share. 如果有人知道如何为最新的Java做类似的技巧,请分享。

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

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