If you have migrated / upgraded your project to Spring Framework 3.2 and project builds successfully but when application starts, you are getting IncompatibleClassChangeError, then this post will help you to resolve the same.

Tools and Technologies used in this article

  1. Spring Framework 3.2.3
  2. Spring Security 3.1.4

Error Stack-trace

Server Console

Caused by: java.lang.IncompatibleClassChangeError: class org.springframework.core.type.classreading.ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor as super class
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)

Solution

In Spring Framework 3.2, spring-asm is upgraded to org.objectweb.asm 4.0 and included in spring-core. Now there is no need to add this spring-asm3 dependency separately within the build script. If it is declared, then remove the same while migrating to 3.2.
There are chances that your project is using other spring extension (say Spring Security 3.1.4), which is dependent on spring-asm3. As a result different versions of asm module may be available in the classpath.
When application starts, different versions of classes may get loaded - ClassMetadataReadingVisitor from spring-core (containing asm 4.0) and ClassVisitor from spring-asm (containing asm 3.x). As a result ClassMetadataReadingVisitor violates the contract of interface ClassVisitor and resulting the IncompatibleClassChangeError.

To avoid this, you need to add spring-asm to the exclusion list as shown below.

<exclusions>
    <exclusion>
        <groupId>org.springframework</groupId>
        <artifactId>spring-asm</artifactId>
    </exclusion>
</exclusions> 

Check for spring-asm Dependency

Go to the project directory in the command prompt and execute mvn dependency:tree

Command Prompt

static-122:SpringSecurityHelloWorld srccodes$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building SpringSecurityHelloWorld 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ SpringSecurityHelloWorld ---
[WARNING] The POM for org.apache.maven:maven-artifact:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] The POM for org.apache.maven:maven-plugin-api:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] The POM for org.apache.maven:maven-project:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] The POM for org.apache.maven:maven-model:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] The POM for org.apache.maven:maven-core:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO] com.srccodes.spring:SpringSecurityHelloWorld:war:0.0.1-SNAPSHOT
[INFO] +- org.springframework:spring-webmvc:jar:3.2.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.2.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:3.2.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:3.2.3.RELEASE:compile
[INFO] |  |  \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  +- org.springframework:spring-expression:jar:3.2.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:3.2.3.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-core:jar:3.1.4.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:3.0.7.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-asm:jar:3.0.7.RELEASE:compile
[INFO] |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- org.springframework.security:spring-security-web:jar:3.1.4.RELEASE:compile
[INFO] |  +- org.springframework:spring-jdbc:jar:3.0.7.RELEASE:compile
[INFO] |  \- org.springframework:spring-tx:jar:3.0.7.RELEASE:compile
[INFO] \- org.springframework.security:spring-security-config:jar:3.1.4.RELEASE:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.777s
[INFO] Finished at: Sun Jun 09 20:54:20 GMT+05:30 2013
[INFO] Final Memory: 6M/81M
[INFO] ------------------------------------------------------------------------

From the above dependency check, we can see that old spring-asm (org.springframework:spring-asm:jar:3.0.7.RELEASE) will also be loaded along with the new asm included in spring-core (org.springframework:spring-core:jar:3.2.3.RELEASE.

Exclude spring-asm Dependency

Now add spring-asm in the exclusion list of each dependent dependency (for example spring-security-core).

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.srccodes.spring</groupId>
  <artifactId>SpringSecurityHelloWorld</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringSecurityHelloWorld</name>
  <url>http://maven.apache.org</url>
   
  <properties>
        <org.springframework.version>3.2.3.RELEASE</org.springframework.version>
        <spring-security.version>3.1.4.RELEASE</spring-security.version>
    </properties>
 
    <dependencies>
        <!-- Spring MVC depends on spring-core, spring-beans, spring-context, spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
         
        <!-- Spring Security Dependencies -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring-security.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-asm</artifactId>
                </exclusion>
            </exclusions>
         </dependency> 
         <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring-security.version}</version>
          </dependency> 
          <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring-security.version}</version>
          </dependency>
    </dependencies>
 
    <build>
        <finalName>SpringSecurityHelloWorld</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Run the maven dependency command again to verify.
Command Prompt

static-122:SpringSecurityHelloWorld srccodes$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building SpringSecurityHelloWorld 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ SpringSecurityHelloWorld ---
[WARNING] The POM for org.apache.maven:maven-artifact:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] The POM for org.apache.maven:maven-plugin-api:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] The POM for org.apache.maven:maven-project:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] The POM for org.apache.maven:maven-model:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] The POM for org.apache.maven:maven-core:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO] com.srccodes.spring:SpringSecurityHelloWorld:war:0.0.1-SNAPSHOT
[INFO] +- org.springframework:spring-webmvc:jar:3.2.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.2.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:3.2.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:3.2.3.RELEASE:compile
[INFO] |  |  \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  +- org.springframework:spring-expression:jar:3.2.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:3.2.3.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-core:jar:3.1.4.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:3.0.7.RELEASE:compile
[INFO] |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- org.springframework.security:spring-security-web:jar:3.1.4.RELEASE:compile
[INFO] |  +- org.springframework:spring-jdbc:jar:3.0.7.RELEASE:compile
[INFO] |  \- org.springframework:spring-tx:jar:3.0.7.RELEASE:compile
[INFO] \- org.springframework.security:spring-security-config:jar:3.1.4.RELEASE:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.729s
[INFO] Finished at: Sun Jun 09 20:56:58 GMT+05:30 2013
[INFO] Final Memory: 6M/81M
[INFO] ------------------------------------------------------------------------

Hopefully, this will resolve your IncompatibleClassChangeError issue, just like mine.

References