Search code examples
javaazuregradleintellij-idea

Getting an UnsupportedClassVersionError after updating Java 17 to Java 21


I have updated my Java project from version 17 to 21.

My environment variables for my JAVA_HOME has been set to where the Java 21 folder is.

Variable Name: JAVA_HOME
Variable Value: C:\Java\jdk-21.0.2

I have confirmed both Gradle and Java are on the correct versions:

$ java --version
openjdk 21.0.2 2024-01-16
OpenJDK Runtime Environment (build 21.0.2+13-58)
OpenJDK 64-Bit Server VM (build 21.0.2+13-58, mixed mode, sharing)

$ gradle --version
------------------------------------------------------------
Gradle 8.10
------------------------------------------------------------

In my Intellij project's gradle.properties file, I have specified the sourceCompatibility as so:

sourceCompatibility = JavaVersion.VERSION_21

I am using the latest version of gradle, 8.10, and have the following in my gradle-wrapper.properties file:

distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip

In Intellij, I have Gradle JVM referencing Java 21

enter image description here

My Java Compiler is referencing Java 21

enter image description here

I was able to run a gradle build successfully.

But when I look at my External Libraries, I see my previous Java 17 library is still there, but its now referencing my Java 21 file directory.

enter image description here

When I push this up onto my Azure app service, I see the following logs:

2024-09-11T21:25:28.202408129Z 2024-09-11 21:25:28.202Z INFO  c.m.applicationinsights.agent - Java version: 17.0.12, vendor: Eclipse Adoptium, home: /opt/java/openjdk
2024-09-11T21:25:28.721339850Z Exception in thread "main" java.lang.UnsupportedClassVersionError: com/xxx/yyy/Application 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

Is this issue on Azure, as I definitely dont have my JAVA_HOME set as the following directory as per the log message above, "/opt/java/openjdk"

What am I missing here for me to finish my Java 17 to Java 21 upgrade?




SOLUTION https://tomcat.apache.org/download-11.cgi https://tomcat.apache.org/whichversion.html

The issue was an older Apache Tomcat running on the Azure server that did not support Java 21. Updating Apache Tomcat to a later version resolved the issue.


Solution

  • Azure supports Java 17, not Java 21

    Your log message tells you exactly the problem:

    2024-09-11T21:25:28.721339850Z Exception in thread "main" java.lang.UnsupportedClassVersionError: com/xxx/yyy/Application 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

    You compiled your code for a later version of Java than is supported by your Azure runtime.

    To translate that "class file version" number 61.0 to a version of Java, see the Java version history page at Wikipedia.

    👉🏽 Class file format 61 is used by Java 17.

    Change your Gradle configuration to compile for Java 17. You should be able to continue with Java 21 while compiling for Java 17.

    Java Class File Format
    17 61
    21 65

    By the way, IntelliJ can download and install a JDK for you.


    I tried searching the Azure documentation sites for system requirements, and minimum Java supported, but could not locate such basic info.