[Solved] Parboiled under Java 21: Unsupported class file major version 65 – Java

by
Alexei Petrov
java-21 parboiled

Quick Fix: In the build.gradle file, configure Gradle to force the resolution of all ASM dependencies to version 9.5 or above. This ensures compatibility with Java 21, which requires a newer version of ASM.

The Problem:

Parboiled, when upgraded from Java 17 to 21, causes a runtime error. Error message: "Unsupported class file major version 65" is reported when attempting to create a parser and is attributed to the parser class being compiled with target 21, version 65. The Java version upgrade resulted in the parser class being compiled with target 21, which is not compatible with parboiled.

The Solutions:

Solution 1: Use ASM 9.5

Parboiled depends on the ASM library, which is used to generate Java bytecode. Unfortunately, ASM 9.2, which is used by Parboiled 1.4.1, does not support Java 21. To fix this issue, you can force Gradle to resolve all ASM dependencies to version 9.5, which does support Java 21.

To do this, add the following to your build.gradle file:

configurations.all {
    resolutionStrategy.eachDependency { details ->
        if (details.requested.group == 'org.ow2.asm') {
            details.useVersion "9.5"
        }
    }
}
dependencies {
    implementation 'org.parboiled:parboiled-java:1.4.1'
}

Using this approach, you can continue to use Parboiled 1.4.1 with Java 21.

Solution 2: Use an older version of Parboiled

If you are unable to find a workaround, you can use an older version of Parboiled that is compatible with Java 21. Parboiled versions 1.2.4 and earlier are compatible with Java 21. You can add the following dependency to your build.gradle file to use Parboiled 1.2.4:

dependencies {
    implementation 'org.parboiled:parboiled-java:1.2.4'
}

Once you have added the dependency, you will need to rebuild your project.

Q&A

What will happen if I compile with JavaSE-17 instead of Java 21?

You won’t get any compilation errors.

What will happen if I use ASM 9.5 or newer?

The compilation issue will be resolved.

What is the workaround for the problem?

Force Gradle to use ASM 9.5 or later.