I have some code which uses cucumber-java8 with serenity and run using Java 17. I now want to upgrade to Java 21, but when I attempt to run the cucumber file (through mvn test
), I get the following error during compilation:
Caused by: java.lang.UnsupportedOperationException: Cannot define class using reflection: Unable to make protected java.lang.Package java.lang.ClassLoader.getPackage(java.lang.String) accessible: module java.base does not "opens java.lang" to unnamed module @6bc168e5
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Initializable$Unavailable.defineClass(ClassInjector.java:472)
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.injectRaw(ClassInjector.java:284)
at net.bytebuddy.dynamic.loading.ClassInjector$AbstractBase.inject(ClassInjector.java:118)
at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$InjectionDispatcher.load(ClassLoadingStrategy.java:241)
at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default.load(ClassLoadingStrategy.java:148)
at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:101)
at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:6166)
at net.serenitybdd.rest.utils.RestSpecificationFactory.\<clinit\>(RestSpecificationFactory.java:60)
at net.serenitybdd.rest.utils.RestDecorationHelper.decorate(RestDecorationHelper.java:20)
at net.serenitybdd.rest.SerenityRest.fetchRequestSpecification(SerenityRest.java:233)
at net.serenitybdd.rest.SerenityRest.given(SerenityRest.java:220)
It works again when I switch back to Java 17.
<cucumber.version>7.9.0</cucumber.version>
<junit.version>4.13.2</junit.version>
<junit.jupiter.version>5.9.0</junit.jupiter.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<serenity.version>4.1.6</serenity.version>
<webdriver.driver>provided</webdriver.driver>
<maven.surefire.version>3.5.2</maven.surefire.version>
<java.version>21</java.version>
Thank you for your time and comments. I did try all the stuff before posting here, including upgrading all the libraries to the latest version (serenity, cucumber and lombok as well). It turns out there was one dependency that I hadn't added (which seemed to work out of the box in Java 17, but not so in Java 21) which was:
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.16.1</version>
Adding this has resolved the issue and the scripts are running well. I am new to both java and selenium/serenity, hence was wondering if the days I spent attempting the fix was done wrongly. I thank you all for your support and guidance. The help notes, especially about the upgrade, helped a lot since this time, I paid attention to the errors.