Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Class<T> static method forName() IncompatibleClassChangeError

Tags:

java

private static Importable getRightInstance(String s) throws Exception { Class c = Class.forName(s).asSubclass(Importable.class); Importable i = c.newInstance(); return i; }

which i can also write

private static Importable getRightInstance(String s) throws Exception {
   Class<? extends Importable> c = (Class<? extends Importable>)Class.forName(s);
   Importable i = c.newInstance();
   return i;
}

or

private static Importable getRightInstance(String s) throws Exception {
   Class<?> c = Class.forName(s);
   Importable i = (Importable)c.newInstance();
   return i;
}

where Importable is an interface and s is a string representing an implementing class. Well, in any case it gives the following:

Exception in thread "main" java.lang.IncompatibleClassChangeError: class C1 has
interface Importable as super class

Here is the last snippet of the stack trace:

 at java.lang.Class.forName(Class.java:169)
 at Importer.getRightImportable(Importer.java:33)
 at Importer.importAll(Importer.java:44)
 at Test.main(Test.java:16)

Now, class C1 actually implemens Importable and i totally don't understand why it complaints.

Thanks in advance.

like image 830
Metz Avatar asked Jul 18 '26 09:07

Metz


1 Answers

IncompatibleClassChangeError means something is wrong with the class file that you're loading. In this case, it sounds like Importable was originally a class when C1 was compiled, and now you've changed it to an interface. Since the JVM cares about the difference between extends SomeClass and implements SomeInterface, you'll need to recompile C1 against the current Importable interface (and probably also change its code from extends to implements.

like image 195
Ken Bloom Avatar answered Jul 20 '26 00:07

Ken Bloom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!