java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.Comparator java.util.TreeMap.comparator accessible:module [duplicate]
Asked Answered
O

8

26

This is my first cucumber project and i followed a tutorial when setting everything up. It all seems to be the same but for some reason i get this:

java.lang.ExceptionInInitializerError. Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.Comparator java.util.TreeMap.comparator accessible: module java.base does not "opens java.util" to unnamed module @74ad1f1f

Any idea how to solve this error ?

Below i have posted everything that comes out in my console as well as my pom file in case there is an issue with my dependencies eventhough the guy from the tutorial's pom file is identical.

This is everything that comes out in my Console.

[31mFailed scenarios:[0m
[31muni/login/Login.feature:3 [0m# Scenario: Enter the system.
1 Scenarios ([31m1 failed[0m)
5 Steps ([31m1 failed[0m, [36m4 skipped[0m)
0m0.185s

java.lang.ExceptionInInitializerError
    at cucumber.deps.com.thoughtworks.xstream.XStream.setupConverters(XStream.java:820)
    at cucumber.deps.com.thoughtworks.xstream.XStream.<init>(XStream.java:574)
    at cucumber.deps.com.thoughtworks.xstream.XStream.<init>(XStream.java:530)
    at cucumber.runtime.xstream.LocalizedXStreams$LocalizedXStream.<init>(LocalizedXStreams.java:50)
    at cucumber.runtime.xstream.LocalizedXStreams.newXStream(LocalizedXStreams.java:37)
    at cucumber.runtime.xstream.LocalizedXStreams.get(LocalizedXStreams.java:29)
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
    at cucumber.runtime.Runtime.runStep(Runtime.java:300)
    at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
    at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
    at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:102)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:95)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:38)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at cucumber.api.junit.Cucumber.run(Cucumber.java:100)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:93)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
    at ✽.Given Потребителят отваря екрана за вход в системата(uni/login/Login.feature:4)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.Comparator java.util.TreeMap.comparator accessible: module java.base does not "opens java.util" to unnamed module @74ad1f1f
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:177)
    at java.base/java.lang.reflect.Field.setAccessible(Field.java:171)
    at cucumber.deps.com.thoughtworks.xstream.core.util.Fields.locate(Fields.java:39)
    at cucumber.deps.com.thoughtworks.xstream.converters.collections.TreeMapConverter.<clinit>(TreeMapConverter.java:50)
    at cucumber.deps.com.thoughtworks.xstream.XStream.setupConverters(XStream.java:820)
    at cucumber.deps.com.thoughtworks.xstream.XStream.<init>(XStream.java:574)
    at cucumber.deps.com.thoughtworks.xstream.XStream.<init>(XStream.java:530)
    at cucumber.runtime.xstream.LocalizedXStreams$LocalizedXStream.<init>(LocalizedXStreams.java:50)
    at cucumber.runtime.xstream.LocalizedXStreams.newXStream(LocalizedXStreams.java:37)
    at cucumber.runtime.xstream.LocalizedXStreams.get(LocalizedXStreams.java:29)
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
    at cucumber.runtime.Runtime.runStep(Runtime.java:300)
    at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
    at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
    at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:102)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:95)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:38)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at cucumber.api.junit.Cucumber.run(Cucumber.java:100)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:93)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

And this is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>uni.ais</groupId>
  <artifactId>first-cucumber-project</artifactId>
  <version>1.1.0-SNAPSHOT</version>
  <name>first-cucumber-project-gr</name>
  <properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.5</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.5</version>
    </dependency>
  </dependencies>
</project>
Onanism answered 18/1, 2022 at 13:32 Comment(4)
Seems like an version incompatibility between the cucumber version and the Java version you are running. It would be helpful if you provided us with both versions.Db
JDK is 17.0.1 / JRE is 1.8.0 and cucumber is 1.0.0.202107150747 which i think is the latest version since i installed it from the eclipse marketplace recently.Onanism
The exception suggests you're not using Java 1.8.Babita
This could be useful: #70413305Savannahsavant
O
14

I solved my problem. Turns out the JRE that eclipse had automatically downloaded and was using wasn't compatible with this version of cucumber. I manually changed the path to a jre 1.8 that i had in my ProgramFilex(x86)/Java folder and now everything works fine.

Onanism answered 19/1, 2022 at 19:22 Comment(3)
can you tell us how to change the path?Sender
Better fix the core of the problem. Downgrading the Java version is not a permanent solution.Senary
Sorry i just now saw the question. I don't remember exactly what i did but i think if you go in the menus in Window > Preferences, then go to Java > Installed JREs you can add the path to your jre and the one i am using that worked with that version of cucumber is jre1.8.0. Also i know that using the older version of cucumber isn't a good idea but we had an assignment in uni that required us to use that specific version.Onanism
S
29

It need to add some instructions to the jvm options.
I added it on idea2022, it's my steps.
Path: Run—>EditConfigurations…—>Modify options—>JAVA Add VM options—>VM options
Options: --add-opens java.base/java.util=ALL-UNNAMED
By the way, it has other options:

--add-opens java.base/java.lang=ALL-UNNAMED
--add-opens java.base/java.nio=ALL-UNNAMED
--add-opens java.base/sun.nio.ch=ALL-UNNAMED

Hope it can help you to reslove the problem

Sackcloth answered 7/12, 2022 at 10:3 Comment(3)
I am missing the part where you explain why one would do this.Cecilius
@MartijnJanJaapdeBruin from the exception msg: java.lang.ExceptionInInitializerError. Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.Comparator java.util.TreeMap.comparator accessible: module java.base does not "opens java.util" to unnamed module @74ad1f1f The message says java.base needs to opens java.util. So you need the JVM arg: --add-opens java.base/java.util=ALL-UNNAMED This will reveal another package that needs another --add-opens entry. But the key is read the msg and add an appropriate --add-opens JVM arg.Anatole
is there any other way to make it work without passing these arguments while using java 17?Phip
O
14

I solved my problem. Turns out the JRE that eclipse had automatically downloaded and was using wasn't compatible with this version of cucumber. I manually changed the path to a jre 1.8 that i had in my ProgramFilex(x86)/Java folder and now everything works fine.

Onanism answered 19/1, 2022 at 19:22 Comment(3)
can you tell us how to change the path?Sender
Better fix the core of the problem. Downgrading the Java version is not a permanent solution.Senary
Sorry i just now saw the question. I don't remember exactly what i did but i think if you go in the menus in Window > Preferences, then go to Java > Installed JREs you can add the path to your jre and the one i am using that worked with that version of cucumber is jre1.8.0. Also i know that using the older version of cucumber isn't a good idea but we had an assignment in uni that required us to use that specific version.Onanism
L
6

Adding to what @GhostXu wrote, if you are using Maven you can add an <argLine> to your surefile config in your pom.xml. Something like

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <configuration>
    <argLine>--add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED</argLine>
  </configuration>
</plugin>
Larainelarboard answered 16/12, 2022 at 13:53 Comment(0)
E
3

If using Gradle instead of maven, you can add the following block to your build.gradle file to auto add the appropriate JVM arg(s) to your tests when running instead of manually configuring. This can be used as a stop gap until a new library version is released or you refactor your tests to be compliant with the new standards, depending on your situation.

tasks.withType(Test).configureEach {
    jvmArgs = jvmArgs + ['--add-opens=java.base/java.util=ALL-UNNAMED']
}
Enclasp answered 6/9, 2023 at 15:15 Comment(0)
A
2

If you are using Apache Tomcat then add the following to the catalina.sh file. Search JDK_JAVA_OPTIONS word and add the following

# Add the JAVA 9 specific start-up parameters required by Tomcat
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.lang=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.io=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.util=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.util.concurrent=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens java.base/java.math=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.net=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.text=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.sql/java.sql=ALL-UNNAMED"
Ancestral answered 10/10, 2023 at 7:51 Comment(0)
I
1

I had the same problem with JDK 17 while using Maven project.

I will provide you with the dependencies that worked for me along with the VM argument i had to use.

VM arguments(From Run Configurations):

--add-opens java.base/java.lang=ALL-UNNAMED

Dependencies (in Pom.xml):

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>2.0.9</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito2</artifactId>
        <version>2.0.9</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
Infusorian answered 9/11, 2023 at 1:1 Comment(0)
R
0

If you are using gradle project just add in build.gradle

test
{
  jvmArgs '--add-opens=java.base/java.lang=ALL-UNNAMED'
}
Rothko answered 23/1 at 10:51 Comment(0)
G
-2

I have faced the same issue. On my PC java was not properly configured in Environment Variables, once I added the Java path in Environment Variables > User variables. it worked fine.

enter image description here

Gridley answered 20/11, 2023 at 6:21 Comment(1)
Suggesting someone uses a non-LTS version of Java that's really quite old is not a solution to this problem. I'd suggest you delete your answer.Goldshell

© 2022 - 2024 — McMap. All rights reserved.