java ClassLoader.defineClass 抛出的 ClassCircularityError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6179533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:43:21  来源:igfitidea点击:

ClassCircularityError thrown by ClassLoader.defineClass

javacircular-dependencyclassloader

提问by Jim

I'm loading classes using a custom class loader. For the most part, everything works, but sometimes when I load particularly complex projects/libraries, I get a strange bug:

我正在使用自定义类加载器加载类。在大多数情况下,一切正常,但有时当我加载特别复杂的项目/库时,我会遇到一个奇怪的错误:

Exception in thread "main" java.lang.ClassCircularityError: 
  org/apache/commons/codec/binary/Hex
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:466)
    at my.custom.class.Loader.loadClass(...)

Looking at the Javadocs, I wouldn't expect defineClassto throw this particular error. org/apache/commons/codec/binary/Hexis the class I'm trying to load. It's almost as if defineClasswants a copy of the class before it'll define the class - which makes no sense to me.

查看 Javadocs,我不希望defineClass抛出这个特定的错误。 org/apache/commons/codec/binary/Hex是我正在尝试加载的课程。这几乎就像defineClass在定义类之前想要类的副本一样 - 这对我来说没有意义。

Ideas?

想法?

回答by Pa?lo Ebermann

A ClassCircularityErroris thrown when some class is a (indirect) superclass of itself, some interface (indirectly) extends itself or similar.

ClassCircularityError当某个类是其自身的(间接)超类、某个接口(间接)扩展自身或类似时,会抛出A。

This should normally not occur as a well-behaved compiler will not produce such classes, but using different versions of a library (or using several libraries containing different versions of a class) could bring this problem.

这通常不会发生,因为行为良好的编译器不会生成此类类,但使用不同版本的库(或使用包含不同版本类的多个库)可能会带来此问题。

Scan your libraries for double class names, in particular have a look if there are multiple versions of the mentioned org.apache.commons.codec.binary.Hexclass.

扫描您的库中是否有双类名称,特别是看看是否有提到的org.apache.commons.codec.binary.Hex类的多个版本。