Skip to content

Spring Class Version Mismatch: Fixing 'class file has wrong version' Error

Problem Statement

When compiling a Java project with Spring, you encounter the error:
java: cannot access org.springframework.beans.factory.annotation.Autowired
class file has wrong version 61.0, should be 55.0

This typically occurs in environments mixing Java 11 (Java class version 55) with libraries compiled for Java 17+ (Java class version 61). The core issue stems from version mismatches between Java runtime, Spring Framework, build tools, and IDE configurations.

Key Symptoms

  • Compilation fails on Spring annotations (@Autowired, @JsonTest, etc.)
  • Mismatched Java class version numbers in error message
  • Occurs despite having Java 11 correctly installed and JAVA_HOME set

Understanding the Root Cause

Java class versions correspond to major Java releases:

  • 55.0 → Java 11
  • 61.0 → Java 17

Spring Framework 6+ requires Java 17 and produces class files incompatible with Java 11 runtimes. This error commonly appears when:

  • Using Spring Boot 3+ with Java 11
  • Having incorrect build tool configurations
  • IDE using different JDK than command line

Spring Version Compatibility

1. Align Java and Spring Versions

Option A: Upgrade to Java 17 (Recommended)

  • Install JDK 17 and update environment variables:
    bash
    # Check current JDK
    java -version
    
    # Set JAVA_HOME (example for macOS/Linux)
    export JAVA_HOME="/path/to/jdk-17"
    
    # Verify change
    echo $JAVA_HOME

Option B: Downgrade to Spring Boot 2.7

  • Update your Maven pom.xml:
    xml
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version> <!-- Compatible with Java 11 -->
    </parent>
  • Avoid automatic parent inheritance if using Spring Initializr with Java 11.

2. Configure IntelliJ IDEA Correctly

  1. Set Project SDK:
    File → Project Structure → Project Settings → Project
    Select JDK 17 (if using Spring 6) or JDK 11 (if using Spring 5.3)

  2. Configure Modules:
    Project Structure → Modules → Sources Tab
    Ensure Language level matches JDK version

  3. Maven Runner Settings:
    Preferences → Build, Execution, Deployment → Build Tools → Maven → Runner
    Set JRE to JDK 17 (not SDK default)

WARNING

IntelliJ's embedded terminal may inherit environment variables differently than system terminals. Always verify using:

bash
echo $JAVA_HOME
mvn -v

3.Maven/Gradle Config Verification

For Maven:

xml
<properties>
    <java.version>17</java.version> <!-- Or 11 if downgrading Spring -->
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

For Gradle (build.gradle):

gradle
java {
    sourceCompatibility = JavaVersion.VERSION_17 // Or VERSION_11
    targetCompatibility = JavaVersion.VERSION_17
}

4. Clean Build Artifacts

Delete problematic dependencies:

bash
# Remove Maven local repository cache
rm -rf ~/.m2/repository/org/springframework

# Rebuild project
mvn clean install

In IntelliJ:
Right-click project → Maven → Reimport

Advanced Troubleshooting

Consistency Checklist

  • Verify Java version matches everywhere:
    bash
    java -version             # Terminal
    echo $JAVA_HOME           # Environment variable
  • Ensure SDK matches in:
    • IDEA Project Structure
    • Maven/Gradle settings
    • Run/Debug configurations

Eclipse Users:
Update JRE for Maven executions via:
Run → Run Configurations → Maven Build → JRE tab

Conclusion

Resolve class file has wrong version errors by:

  1. Matching Spring Boot versions to your Java runtime:
    • Java 17 → Spring Boot 3+
    • Java 11 → Spring Boot 2.7
  2. Ensuring consistent JDK settings across environment variables, build tools, and IDEs
  3. Cleaning cached dependencies when changing versions

Critical Note

Spring Boot 3+ will not work with Java 11 regardless of environment configurations. Always align major dependency versions before debugging.

Most solutions involve correcting version mismatches rather than complex code changes. Start with verifying Java/Spring compatibility, then progress to build environment checks if issues persist.