简体   繁体   English

具有相同名称的两个相同类之间的Java差异

[英]Java difference between two identical classes with identical name

I have a class A: 我有A班:

public class A implements I {}

Interface I is a class coming from a jar dependency. 接口I是来自jar依赖项的类。
I can compile this code to a jar without any compilation issues. 我可以将此代码编译到jar中,而没有任何编译问题。
I deployed the jar file on our server so that an application can use it. 我将jar文件部署在我们的服务器上,以便应用程序可以使用它。 The application runs the following code 该应用程序运行以下代码

I instance = (I) someObject.getImplementationViaJNDI();

The method uses JNDI and RMI to get an instance of my interface implementation and then throws a java.lang.ClassCastException my.domain.name.A cannot be cast to my.differentdomain.name.I 该方法使用JNDI和RMI获取我的接口实现的实例,然后抛出java.lang.ClassCastException my.domain.name.A cannot be cast to my.differentdomain.name.I
I am pretty sure the problem does not lie within JNDI or RMI because the application logs all the implemented methods of my class and the name, however it also logs the following 我很确定问题不在JNDI或RMI内,因为该应用程序记录了我的类和名称的所有已实现方法,但是它还会记录以下内容

Object my.domain.name.A implements interface: java.lang.Class

The application that tries to cast my implementation has a version of the jar file containing Interface I on its classpath, however it does not recognize that it is implemented by class A . 尝试强制实现的应用程序的jar文件版本在其类路径上包含Interface I ,但是它无法识别它是由A类实现的。

I wonder how java differentiates between two identical classes with the same fqdn and how the application gets to think my class implements interface java.lang.Class . 我想知道java如何区分具有相同fqdn的两个相同类,以及应用程序如何认为我的类实现了接口java.lang.Class

Thanks to @Jens I found the solution to my problem. 感谢@Jens,我找到了解决问题的方法。
The jar file I generated has all the dependencies packed within itself, including the package with the interface. 我生成的jar文件内部包含所有依赖项,包括带有接口的软件包。
The application which loads my jar file has the interface already loaded. 加载我的jar文件的应用程序已经加载了接口。 But then while my jar file is being loaded, it loads the interface again, but with another classloader. 但是,当我的jar文件被加载时,它又使用另一个类加载器再次加载了接口。
I solved the error by not packing the interface in my jar so that it started using the interface class already loaded by the loading application. 我通过不将接口包装在我的jar中来解决该错误,从而使它开始使用正在由加载应用程序加载的接口类。

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

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