Hello guys, The "class file has wrong version" error typically occurs when you're trying to run a Java application compiled with a higher Java version than the one supported by your current Java runtime environment. In this case, the error message suggests that the class file was compiled with Java version 17 (version 61.0) but is being executed on a Java runtime that only supports version 9 (version 53.0). This error commonly asked when you try to use Spring Framework 6 or Spring Boot 3 in your project which is not running on Java 16 or Java 17. The error also manifest in different messages like "“Java class file has wrong version 61.0” or "“Java class file has wrong version 62.0”.
For example, when you use Spring Boot 3 or Spring Framework 6 with Java 8 you will get " class file has wrong version 61.0, should be 52.0" because for Java 8 compile maximum class file version is 52.0, it doesn't have any information of higher class file version.
Similarly, if you use spring Framework 6 or Spring Boot 3 with Java SE 11 then you will get “class file has wrong version 61.0, should be 55.0” because for Java 11 compiler the class file version is 55.0 and it doesn't know about version Java 17 class file version which is 61.0
How to fix " class file has wrong version 61.0, should be 52.0"
To fix this error in a Spring Boot application, you can follow these steps:
1. Check Your Java Version
First, verify the version of Java installed on your system. Open a terminal or command prompt and run the following command to check your Java version:
$ java -version
Make sure you have a compatible Java version installed. If you need to update your Java version, download and install a newer version that supports at least Java 17.
2. Update Your Build Configuration
Update your build configuration (e.g., Maven or Gradle) to specify the Java version you want to use for compilation. For example, if you're using Maven, you can add the following lines to your pom.xml:
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
If you're using Gradle, you can add similar configuration in your build.gradle:
compileJava {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
Replace 17 with the appropriate Java version that you have installed.
3. Rebuild Your Project
After updating your build configuration, rebuild your project to recompile your code with the correct Java version. If you're using Maven, you can run the following command:
$ mvn clean install
For Gradle, you can use:
$ ./gradlew clean build
4. Run Your Spring Boot Application
Once your project is successfully rebuilt, you should be able to run your Spring Boot application without encountering the "class file has wrong version" error.
Example
Recently one of my team member complain about this error while creating a Spring boot project in NetBeans IDE, he was getting following error:
class file has wrong version 61.0, should be 53.0
COMPILATION ERROR :
-------------------------------------------------------------
com/datacat/demo/MyRestController.java:[4,47] cannot access org.springframework.web.bind.annotation.GetMapping
bad class file: C:\Users\codejava\.m2\repository\org\springframework\spring-web\6.0.11\spring-web-6.0.11.jar(/org/springframework/web/bind/annotation/GetMapping.class)
class file has wrong version 61.0, should be 53.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
1 error
-------------------------------------------------------------
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 2.471 s
Finished at: 2023-09-23T14:00:34+05:00
------------------------------------------------------------------------
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project SpringDemo: Compilation failure
com/datacat/demo/MyRestController.java:[4,47] cannot access org.springframework.web.bind.annotation.GetMapping
bad class file: C:\Users\codejava\.m2\repository\org\springframework\spring-web\6.0.11\spring-web-6.0.11.jar(/org/springframework/web/bind/annotation/GetMapping.class)
class file has wrong version 61.0, should be 53.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
Once he updated the Java version to Java 17 on IDE the error went away.
But, if you are using other IDE like Eclipse or IntelliJ IDEA then you can also make the similar changes on their project settings, for example in IntelliJ IDEA you can choose any Java installation for your Java project as shown below:
That's all about how to fix "class file has wrong version 61.0, should be 53.0 error" or “class file has wrong version 61.0, should be 55.0” error in Java while using Spring Framework 6 and Spring Boot 3 . Btw, this error can come with any third party library which is compiled with Java 17 so make sure you use the right Java version while using them.
By following these steps, you should be able to resolve the version mismatch issue and run your Spring Boot application with the correct Java version.
If you are still facing issue even after following these steps and not able to solve the problem then please paste your error message in comments and I will try to find solution for you.
No comments :
Post a Comment