Tag Archives: java.lang.IncompatibleClassChangeError

JUnit Error: java.lang.IncompatibleClassChangeError [How to Solve]

When testing spring with JUnit, an error is reported:

 

 1 java.lang.IncompatibleClassChangeError: class org.springframework.core.LocalVariableTableParameterNameDiscoverer$ParameterNameDiscoveringVisitor has interface org.springframework.asm.ClassVisitor as super class
 2     at java.lang.ClassLoader.defineClass1(Native Method)
 3     at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
 4     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
 5     at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
 6     at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
 7     at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
 8     at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
 9     at java.security.AccessController.doPrivileged(Native Method)
10     at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
11     at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
12     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
13     at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
14     at org.springframework.core.LocalVariableTableParameterNameDiscoverer.inspectClass(LocalVariableTableParameterNameDiscoverer.java:114)
15     at org.springframework.core.LocalVariableTableParameterNameDiscoverer.getParameterNames(LocalVariableTableParameterNameDiscoverer.java:87)
16     at org.springframework.core.PrioritizedParameterNameDiscoverer.getParameterNames(PrioritizedParameterNameDiscoverer.java:65)
17     at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:182)
18     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
19     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
20     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
21     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
22     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
23     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
24     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
25     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
26     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
27     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
28     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
29     at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
30     at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
31     at com.it.sxd.SayHelloTest2.testSayHello(SayHelloTest2.java:11)
32     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
33     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
34     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
35     at java.lang.reflect.Method.invoke(Method.java:606)
36     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
37     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
38     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
39     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
40     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
41     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
42     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
43     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
44     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
45     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
46     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
47     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
48     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
49     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
50     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
51     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
52     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
53     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
54     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

 

This error has a class called classmetadatareadingvisitor, which takes an interface called classvisitor as the parent class. But as we all know, the relationship between classes and interfaces in Java can only be implementation, not inheritance

Then this is the conflict between shelves and bags:

Just remove this rack bag

[How to Solve] java.lang.IncompatibleClassChangeError

 

Today, after sorting out the pom.xml file of maven, we integrated multiple projects with Maven. As a result, when we started tomcat, we encountered a wonderful error:
when we started Tomcat

Warning: Servlet[spring]inwebapplication[/AbcWeb]threwload()exception
java.lang.IncompatibleClassChangeError:classorg.springframework.core.type.classreading.ClassMetadataReadingVisitorhasinterfaceorg.springframework.asm.ClassVisitorassuperclass
	atjava.lang.ClassLoader.defineClass1(NativeMethod)
	atjava.lang.ClassLoader.defineClass(ClassLoader.java:800)
	atjava.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	atorg.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2495)
	atorg.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:859)
	atorg.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1301)
	atorg.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
	atjava.lang.ClassLoader.defineClass1(NativeMethod)

Restart Tomcat, Project>Clean are played all over, but did not find where the problem is. So Google it, the results from this page found this paragraph:

Your newly packaged libraryis not backward binary compatible(BC) with old version. For this reason some of the library clients that are not recompiled may throw the exception.

This is acompletelist of changes in Java library API that may cause clients built with an old version of the library to throw java.lang.IncompatibleClassChangeErrorif they run on a new one (i.e. breaking BC):

Non-final field become static,

Non-constant field become non-static,

Class become interface,

Interface become class,

if you add a new field to class/interface (or add new super-class/super-interface) then a static field from a super-interface of a client class C may hide an added field (with the same name) inherited from the super-class of C (very rare case).

In fact, a closer look reveals that the error description is actually quite clear:

java.lang.IncompatibleClassChangeError:classorg.springframework.core.type.classreading.ClassMetadataReadingVisitorhasinterfaceorg.springframework.asm.ClassVisitorassuperclass

That is to say, has a class called classmetadatareadingvisitor, which takes an interface called classvisitor as its parent class . But we all know that the relationship between class and interface in java can only be realized, not inherited . So why is this class?I try to open this error reporting class in Eclipse:

As you can see, two versions of classmetadatareadingvisitor classes (3.2.6 and 4.0.0) can be found in my workspace. Open these two classes respectively (Maven will download the source code automatically). You can see that the class declaration is:

classClassMetadataReadingVisitorextendsClassVisitorimplementsClassMetadata

So I opened the classvisitor class mentioned in the error description, and the result is as follows:

As you can see, eclipse found three such classes in workspace, and the package name and class name are all exactly the same. As you can see, in version 3.1.4 of spring, this class called classvisitor is actually an interface, which is placed in the spring ASM module. In versions 3.2.6 and 4.0.0, the classvisitor becomes an abstract class

After finding this difference, I checked the version of my spring core module:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.6</version>
</dependency>

But at the same time, there is a dependency of such a package in my pom.xml

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>

It happens that the 3.1.4 interface is in workspace, so the classvisitor is included in both packages. After deleting the spring ASM module, the error disappears and the problem is solved

The spring ASM package doesn’t know when it was imported, which tells us:

when using Maven to process dependent packages, you must not arbitrarily add dependencies. Only used packages are imported, and statements that do not use packages should be deleted in time

when integrating multiple Maven projects, you should pay attention to the version of the dependency package, because some classes (fields that may be included in the package) may have different declarations in different versions, and wrong class or field declarations will lead to incompatible classchangeerror