A Java app fails at startup with UnsupportedClassVersionError. What does it mean and how do you fix it?
IntermediateTroubleshootingJava builds (Maven, Gradle, JAR, WAR)
Answer
Short answer
It means the code was compiled with a newer Java version than the one running it. For example, built with Java 21 (class file version 65) but started on Java 17 (supports up to 61). Fix it by running on the same or newer Java version, or compile for the older target.
The error
java.lang.UnsupportedClassVersionError: com/shop/OrdersApplication has been compiled by a
more recent version of the Java Runtime (class file version 65.0), this version of the
Java Runtime only recognizes class file versions up to 61.0
Class file versions to remember
| Java | Class file version |
|---|---|
| 8 | 52 |
| 11 | 55 |
| 17 | 61 |
| 21 | 65 |
How to fix
- Check the runtime:
java -versionon the server, or the base image tag in the Dockerfile (eclipse-temurin:17-jre). - Check the build target in pom.xml:
<java.version>21</java.version>or<maven.compiler.release>21</maven.compiler.release>. - Make them match — usually by upgrading the runtime image to
eclipse-temurin:21-jre.
Prevention
Pin the same Java version in three places: pom.xml, the CI setup-java step and the Dockerfile base image.
What to say in the interview
"UnsupportedClassVersionError means a build/runtime Java mismatch — compiled on a newer JDK than the runtime. I compare the class file version in the error with java -version, then align the runtime image with the compile target."
All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Java apps: Maven, Gradle, JAR & WAR
More on Java builds (Maven, Gradle, JAR, WAR)
What is pom.xml and what is it used for?BeginnerHow do you build and run a Java project with Maven?BeginnerWhat is the difference between a JAR and a WAR file?BeginnerWhat is Gradle and how is it different from Maven?BeginnerHow do you configure a Spring Boot application for different environments?BeginnerA Maven build fails in the pipeline with "Could not resolve dependencies". How do you fix it?Intermediate