Question
How can I resolve the 'class file has wrong version 61.0, should be 55.0' error when using Spring with Maven in IntelliJ IDEA?
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Answer
This error occurs when there is a Java version mismatch within your project dependencies. Specifically, class file version 61.0 corresponds to Java 17, while version 55.0 corresponds to Java 11. Ensure your project and dependencies align with the Java version you are using.
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
Causes
- Using libraries compiled with a higher Java version than your project (e.g., Spring library compiled with Java 17 while you are using Java 11).
- Classpath is not set correctly, causing IntelliJ to use the wrong version of Java for your project.
- Maven is configured to use an outdated or incorrect JDK version.
Solutions
- Verify your Maven dependency versions and make sure they are compatible with Java 11. Use Spring versions that support Java 11, such as Spring Boot 2.5.x.
- Check your project's Maven POM file for the <properties> section. Ensure it has the following: <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target>.
- In IntelliJ, go to File ➜ Project Structure ➜ Project, and set the Project SDK to Java 11 and the Project language level to 11 (11 - 11.0+ LTS).
- Re-import your Maven project in IntelliJ by right-clicking on the project in the Maven tool window and selecting Reimport to ensure all dependencies are correctly fetched.
- Clear any cached builds by going to Build ➜ Rebuild Project and also consider invalidating caches through File ➜ Invalidate Caches / Restart.
Common Mistakes
Mistake: Not verifying the compatible version of Spring with Java 11.
Solution: Always check the Spring documentation for the version of Spring you are using in relation to your JDK version.
Mistake: Incorrect configuration of IntelliJ settings related to JDK.
Solution: Ensure IntelliJ's Project Structure settings accurately reflect your desired JDK version.
Helpers
- Java version mismatch
- Spring Maven IntelliJ
- class file has wrong version
- fix Java version error
- Spring Boot Java 11