Kohei TAMURA
Kohei TAMURA

Reputation: 5122

How to reproduce ClassCircularityError

Does anyone know how to reproduce java.lang.ClassCircularityError other than throw new ClassCircularityError(...)? If possible, could you show me an example main() program that always occurs the error?

According to Javadoc:

Thrown when the Java Virtual Machine detects a circularity in the superclass hierarchy of a class being loaded.

Upvotes: 1

Views: 1983

Answers (1)

apangin
apangin

Reputation: 98350

This error will not normally happen unless the version of some library used in run-time does not match the version of the library used for compilation.

You can easily reproduce the error using separate compilation.

First, compile A.java with the following definition:

class B {}
class A extends B {}

Then compile B.java with the reverse class hierarchy:

class A {}
class B extends A {}

Finally, combine A.class from the first compilation with B.class from the second compilation. After that, an attempt to run either class will result in

Exception in thread "main" java.lang.ClassCircularityError: A
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

Upvotes: 7

Related Questions