简体   繁体   English

类冲突:两个具有相同类的jar文件

[英]Class conflict: two jar files with the same classes

I have two jar files with similar Util class names, but different method signatures. 我有两个jar文件,它们的Util类名称相似,但方法签名不同。

In jar1, I have a main method which must use the method in Util class in jar1. 在jar1中,我有一个main方法,该方法必须使用jar1中Util类中的方法。 The JVM is linking to Util class in jar2. JVM链接到jar2中的Util类。

How to resolve this class conflict? 如何解决班级冲突?

If both jar files are loaded into the same classloader then there is no way to determine which class will get loaded. 如果两个jar文件都加载到同一个类加载器中,则无法确定要加载哪个类。 The only way to handle this is to isolate them so only one of them is loaded into the classloader you are using. 解决此问题的唯一方法是隔离它们,以便仅将其中一个加载到您正在使用的类加载器中。

You can set up a classloader and only load the jar you want to get the class from, but it is probably much easier to just make sure classes are unique on your path. 您可以设置一个类加载器,并仅加载您要从中获取该类的jar,但是仅要确保类在您的路径上唯一可能要容易得多。

Normally one avoids that situation by using appropriate package names, such that they are different. 通常,通过使用适当的软件包名称来避免这种情况,以使它们不同。

In extreme situations, where you dont have the choice to change the jar files, there is the option "bootclasspath" where you can specify classes that gets loaded first. 在极端的情况下,如果您没有选择更改jar文件的选项,则可以使用“ bootclasspath”选项来指定要首先加载的类。

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

相关问题 使用相同的包和类名动态管理两个jar文件 - Dynamically manage two jar files with the same package and class names 同一jar文件中的两个相同的类 - Two identical classes in the same jar file jar文件在名称空间上发生冲突 - jar files conflict on namespaces 我可以在Tomcat上将相同Java包的类文件拆分为jar文件和classes文件夹吗? - Can I split class files of same java package into jar file and classes folder on Tomcat? 一个类中的类,这两个类中的相同数据成员冲突 - class within a class,conflict of same data member in both these classes 如果在两个jar文件中打包相同的类,将调用哪个java类文件? - Which java class file will be called if same class is packed in two jar files? 我在不同的jar中有两个具有相同名称和相同包结构的类。 是否可以从jar中专门选择一个类? - I have two classes with same name and same package structure in different jars. Is it possbile to choose a class from jar specifically? 如何在同一个端口上部署两个 .jar 文件? - How to deploy two .jar files on the same port? 同一个包的类可以分布在多个Jar文件中吗? - Can classes of same package spread across multiple Jar files? 我可以从同一个jar文件中执行两个不同的类吗? - Can I execute Two different Classes from same jar file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM