Unsupported class file major version 65 with Java 21 "workaround"

8.4k Views Asked by At

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?!?!?

1

There are 1 best solutions below

0
On

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://stackoverflow.com/a/66443594/5389746