简体   繁体   English

在不同的类加载器中摆脱ClassCastException

[英]Get rid of ClassCastException in different class loaders

I have got my own JAR () running via separated class loaders and one JAR ( EhCache ) loaded by shared class loader. 我有自己的JAR()通过分离的类加载器和一个由共享类加载器加载的JAR( EhCache )运行。 So I can access to different cache instances via singleton CacheManager . 所以我可以通过singleton CacheManager访问不同的缓存实例。

How can I cast the same classes from my own JAR among separated class loaders? 如何在分离的类加载器中从我自己的JAR中转换相同的类?

When I'm trying to invoke my own method I have got ClassCastException like class aA is not a type of aA . 当我尝试调用我自己的方法,我也得到了像类ClassCastException异常 aA不是一个类型的aA

PS: Can custom class loader or java reflection helps me? PS:自定义类加载器或java反射可以帮助我吗?

If you really want to be able to share instances between the 2 classloaders, one option is to implement a shared interface. 如果您真的希望能够在两个类加载器之间共享实例,则可以选择实现共享接口。 this shared interface would have to be available from the shared classloader. 这个共享接口必须可以从共享类加载器中获得。

I don't believe you can. 我不相信你能。 As far as I know, if you load the same class using two different class loaders, the two classes are treated as if they were totally unrelated. 据我所知,如果你使用两个不同的类加载器加载同一个类,这两个类被视为完全不相关。

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

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