Video in progress

What causes the UnsupportedClassVersionError?

A class was compiled with a newer Java version, but you are trying to run it on an older JVM. For example, a jar built with Java 21 gives this error on a Java 17 runtime.

📖 Answer

The error text usually looks like this:

java.lang.UnsupportedClassVersionError: com/shop/App 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

The numbers are easy to remember: Java 8 = 52, Java 11 = 55, Java 17 = 61, Java 21 = 65. That is, the Java version + 44.

The fix goes in two directions: either you upgrade the runtime (the base image in the Dockerfile, JAVA_HOME on the server), or you compile for the older version:

<properties>
    <maven.compiler.release>17</maven.compiler.release>
</properties>

The --release flag is better than -source/-target: it also prevents you from using APIs that don't exist in the older version.

In practice this often happens when the CI builds with Java 21 but the server is still on 17. One check: java -version and javap -v App.class | grep major.