Unsupported class file major version 65 with Java 21 "workaround"
Asked Answered
S

3

11

I am using Gradle 8.5 and am specifying...

org.gradle.java.home=/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home

...in my gradle.properties file.

Whenever I introduce a new JAR (for example RDF4J), I get something like the following compilation error:

startup failed:
General error during conversion: Unsupported class file major version 65

java.lang.IllegalArgumentException: Unsupported class file major version 65
at groovyjarjarasm.asm.ClassReader.<init>(ClassReader.java:199)
at groovyjarjarasm.asm.ClassReader.<init>(ClassReader.java:180)
at groovyjarjarasm.asm.ClassReader.<init>(ClassReader.java:166)
at groovyjarjarasm.asm.ClassReader.<init>(ClassReader.java:287)
at org.codehaus.groovy.ast.decompiled.AsmDecompiler.parseClass(AsmDecompiler.java:81)
at org.codehaus.groovy.control.ClassNodeResolver.findDecompiled(ClassNodeResolver.java:251)
at org.codehaus.groovy.control.ClassNodeResolver.tryAsLoaderClassOrScript(ClassNodeResolver.java:189)
at org.codehaus.groovy.control.ClassNodeResolver.findClassNode(ClassNodeResolver.java:169)
at org.codehaus.groovy.control.ClassNodeResolver.resolveName(ClassNodeResolver.java:125)
at org.codehaus.groovy.ast.decompiled.AsmReferenceResolver.resolveClassNullable(AsmReferenceResolver.java:57)
at org.codehaus.groovy.ast.decompiled.AsmReferenceResolver.resolveClass(AsmReferenceResolver.java:44)
at org.codehaus.groovy.ast.decompiled.TypeSignatureParser.visitEnd(TypeSignatureParser.java:110)
...

If I comment out the Java 21 line in my gradle.properties file and compile with, for example, JavaSE-17, I get no errors. The thing I don't understand is if I then:

  1. clean the build
  2. uncomment the Java 21 line
  3. compile (with Java 21)

Everything is fine. What is going on?!?!?

Sortie answered 10/2 at 20:18 Comment(0)
A
7

Do you have sourceCompatibility and targetCompatibility set to 21 in your build.gradle file? https://www.baeldung.com/gradle-sourcecompatiblity-vs-targetcompatibility

I've also had some issues with JAVA_HOME defaulting to the one cached in my .gradle Folder in my user directory even after changing the system variables to point to another JDK. Deleting the .gradle Folder fixed that issue.

You could also try the new Java Toolchain support and set that to 21 https://mcmap.net/q/73493/-how-do-i-tell-gradle-to-use-specific-jdk-version

Autoxidation answered 10/2 at 22:52 Comment(0)
C
0

If using IntelliJ, for spring boot project, change the java version in the pom.xml file to lower version or any version supported. in my case have installed version 21, and in my pom.xml file was like

<properties>
    <java.version>21</java.version>
</properties>

and changed to 17 instead of 21 and it worked successfully.

Caressive answered 30/7 at 8:52 Comment(0)
D
0

There are multiple things to check when updating the Java version.

  1. In your build.gradle file, the sourceCompatibility is referencing the correct Java version
sourceCompatibility = JavaVersion.VERSION_21
  1. In your gradle-wrapper.properties, the distributionUrl is referencing the latest gradle distribution that supports the Java version
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
  1. In your environment variables you have the following variables and they are referencing the files correctly
JAVA_HOME -> The Java SDK folder, ie 
NAME: JAVA_HOME
VALUE: C:\Java\jdk-21.0.2

GRADLE_HOME -> The Gradle folder
NAME: GRADLE_HOME
VALUE: C:\Gradle

Add the Java and Gradle bins location to your Path
C:\Java\jdk-21.0.2\bin
C:\Gradle\gradle-8.10\bin
  1. In your IDE, in this example, Intellij, has the Gradle User Home and Gradle JVM correctly set up to match what you have done above

Intellij Gradle Settings

  1. Library Dependencies in your build.gradle should be updated to the versions that support the latest Java version

Once you got all that done, delete the old gradle files from the .gradle folder, do a gradle clean and a gradle build to see if the change was successful.

Dynamotor answered 11/9 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.