Error running Cucumber tests with Gradle

I got subject error when run cucumber api tests with Gradle via IntelliJ. Below is my edit configurstion:

Error:

May 12, 2022 7:04:29 PM cucumber.runtime.java.ObjectFactoryLoader loadSingleObjectFactory
WARNING: Loading deprecated ObjectFactory from runtime via reflection: cucumber.runtime.SerenityObjectFactory
May 12, 2022 7:04:29 PM com.google.inject.internal.MessageProcessor visit
INFO: An exception was caught and reported. Message: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @1753acfe
java.lang.IllegalStateException: Unable to load cache item
    at com.google.inject.internal.cglib.core.internal.$LoadingCache.createEntry(LoadingCache.java:79)
    at com.google.inject.internal.cglib.core.internal.$LoadingCache.get(LoadingCache.java:34)
......
Caused by: java.lang.ExceptionInInitializerError
    at com.google.inject.internal.cglib.core.$DuplicatesPredicate.evaluate(DuplicatesPredicate.java:104)
    at com.google.inject.internal.cglib.core.$CollectionUtils.filter(CollectionUtils.java:52)
    at com.google.inject.internal.cglib.reflect.$FastClassEmitter.<init>(FastClassEmitter.java:69)
    at com.google.inject.internal.cglib.reflect.$FastClass$Generator.generateClass(FastClass.java:77)
    at com.google.inject.internal.cglib.core.$DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at com.google.inject.internal.cglib.core.$AbstractClassGenerator.generate(AbstractClassGenerator.java:329)
    at com.google.inject.internal.cglib.core.$AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
    at com.google.inject.internal.cglib.core.$AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
    at com.google.inject.internal.cglib.core.internal.$LoadingCache$2.call(LoadingCache.java:54)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at com.google.inject.internal.cglib.core.internal.$LoadingCache.createEntry(LoadingCache.java:61)
    ... 39 more
Caused by: com.google.inject.internal.cglib.core.$CodeGenerationException: java.lang.reflect.InaccessibleObjectException-->Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @1753acfe
    at com.google.inject.internal.cglib.core.$ReflectUtils.defineClass(ReflectUtils.java:464)
    at com.google.inject.internal.cglib.core.$AbstractClassGenerator.generate(AbstractClassGenerator.java:336)
at cucumber.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:258)
	at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:123)
	at cucumber.runtime.Runtime.run(Runtime.java:101)

at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2155)
	at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2045)
	... 35 more
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.google.inject.internal.cglib.core.$MethodWrapper
	at com.google.inject.internal.cglib.core.$DuplicatesPredicate.evaluate(DuplicatesPredicate.java:104)

at cucumber.runner.Runner.runPickle(Runner.java:50)
	... 8 more

Exception in thread "main" cucumber.runtime.CucumberException: com.google.common.util.concurrent.UncheckedExecutionException: java.lang.IllegalStateException: Unable to load cache item
	at cucumber.runtime.Runtime.run(Runtime.java:124)
	at io.cucumber.core.cli.Main.run(Main.java:43)
	at io.cucumber.core.cli.Main.main(Main.java:14)
Caused by: com.google.common.util.concurrent.UncheckedExecutionException: java.lang.IllegalStateException: Unable to load cache item
	at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2051)

	... 2 more
Caused by: java.lang.IllegalStateException: Unable to load cache item
	at com.google.inject.internal.cglib.core.internal.$LoadingCache.createEntry(LoadingCache.java:79)

... 35 more
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.google.inject.internal.cglib.core.$MethodWrapper
	at com.google.inject.internal.cglib.core.$DuplicatesPredicate.evaluate(DuplicatesPredicate.java:104)

My related build.gradle parts:

sourceSets {
    integrationTest {
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
    }
}

configurations {
    cucumberRuntime {
        extendsFrom testImplementation
    }
    integrationTestImplementation.extendsFrom implementation
    integrationTestImplementation.extendsFrom testImplementation
    integrationTestRuntimeOnly.extendsFrom runtimeOnly
}

task integrationTest(type: Test) {
    useJUnitPlatform()
    description = 'Run integration tests.'
    check.dependsOn(it)
    group = 'verification'
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
    shouldRunAfter test
}

tasks.withType(Test) {
    useJUnitPlatform()
    // Prints any test outcomes to the console
    testLogging {
        events "passed", "skipped", "failed"
        showStackTraces true
        showExceptions true
        showCauses true
        exceptionFormat "full"
    }
}

task cucumber() {
    dependsOn assemble, testClasses
//    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'xxx.stepdefinitions', 'src/integrationTest/resources']
        }
    }
}

Error:

May 12, 2022 7:04:29 PM cucumber.runtime.java.ObjectFactoryLoader loadSingleObjectFactory
WARNING: Loading deprecated ObjectFactory from runtime via reflection: cucumber.runtime.SerenityObjectFactory
May 12, 2022 7:04:29 PM com.google.inject.internal.MessageProcessor visit
INFO: An exception was caught and reported. Message: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @1753acfe
java.lang.IllegalStateException: Unable to load cache item
    at com.google.inject.internal.cglib.core.internal.$LoadingCache.createEntry(LoadingCache.java:79)
    at com.google.inject.internal.cglib.core.internal.$LoadingCache.get(LoadingCache.java:34)
......
Caused by: java.lang.ExceptionInInitializerError
    at com.google.inject.internal.cglib.core.$DuplicatesPredicate.evaluate(DuplicatesPredicate.java:104)
    at com.google.inject.internal.cglib.core.$CollectionUtils.filter(CollectionUtils.java:52)
    at com.google.inject.internal.cglib.reflect.$FastClassEmitter.<init>(FastClassEmitter.java:69)
    at com.google.inject.internal.cglib.reflect.$FastClass$Generator.generateClass(FastClass.java:77)
    at com.google.inject.internal.cglib.core.$DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at com.google.inject.internal.cglib.core.$AbstractClassGenerator.generate(AbstractClassGenerator.java:329)
    at com.google.inject.internal.cglib.core.$AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
    at com.google.inject.internal.cglib.core.$AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
    at com.google.inject.internal.cglib.core.internal.$LoadingCache$2.call(LoadingCache.java:54)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at com.google.inject.internal.cglib.core.internal.$LoadingCache.createEntry(LoadingCache.java:61)
    ... 39 more
Caused by: com.google.inject.internal.cglib.core.$CodeGenerationException: java.lang.reflect.InaccessibleObjectException-->Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @1753acfe
    at com.google.inject.internal.cglib.core.$ReflectUtils.defineClass(ReflectUtils.java:464)
    at com.google.inject.internal.cglib.core.$AbstractClassGenerator.generate(AbstractClassGenerator.java:336)

My related build.gradle parts:

sourceSets {
    integrationTest {
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
    }
}

configurations {
    integrationTestImplementation.extendsFrom implementation
    integrationTestImplementation.extendsFrom testImplementation
    integrationTestRuntimeOnly.extendsFrom runtimeOnly
}

task integrationTest(type: Test) {
    useJUnitPlatform()
    description = 'Run integration tests.'
    check.dependsOn(it)
    group = 'verification'
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
    shouldRunAfter test
}

tasks.withType(Test) {
    useJUnitPlatform()
    // Prints any test outcomes to the console
    testLogging {
        events "passed", "skipped", "failed"
        showStackTraces true
        showExceptions true
        showCauses true
        exceptionFormat "full"
    }
}

task cucumber() {
    dependsOn assemble, testClasses
//    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty',
                    '--glue', 'com.anz.periodic.payments.bff.stepdefinitions',
                    'src/integrationTest/resources']
        }
    }

CucumberTest Class

    import org.junit.runner.RunWith;
    
    import io.cucumber.junit.CucumberOptions;
    import net.serenitybdd.cucumber.CucumberWithSerenity;
    
    @RunWith(CucumberWithSerenity.class)
    @CucumberOptions(
        plugin = {"pretty", "html:target/reports/cucumber-html-report",
            "html:target/cucumber-reports/cucumber-pretty",
            "json:target/cucumber.json"},
        tags = {"@test"},
        glue = {"com.abc.stepdefinitions"},
        features = "src/integrationTest/resources/features/")
    public class MyServiceCucumberTests {
    }

My Versions:

Java 17
Gradle 7.4.2
serenityCucumber   : 1.0.29
serenity           : 2.0.90
cucumber        : 4.8.0
IntelliJ Ultimate 2021.1

Further it gives same above error in commandline as well (gradlew cucumber)
However I have same setup with Maven with Java 11 and it runs fine via both IDE and commandline

I tried [this solution](reflection - How to solve InaccessibleObjectException ("Unable to make {member} accessible: module {A} does not 'opens {package}' to {B}") on Java 9? - Stack Overflow) assuming that it is Java version related. But no luck

I managed to run tests via IDE using gradle tasks/ io.cucumber.core.cli.Main. Below are the versions

Java17
Gradle 7.4.2
serenity-cucumber6 - 2.6.0
serenity-rest-assured -  2.6.0

However, if I use gradle task as below it’s giving java.exe error at javaexec in below code

Error: Caused by: org.gradle.process.internal.ExecException: Process 'command 'C:\Java\jdk-17.0.2\bin\java.exe'' finished with non-zero exit value 1

task cucumber() {
    dependsOn assemble, testClasses

    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty',
                    '--glue', 'xxxxx.stepdefinitions',
                    'src/integrationTest/resources']
        }
    }
    finalizedBy aggregate
}