question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting environment fails on Java 17-ea

See original GitHub issue

I have a few tests using the @SetEnvironmentVariable annotation. Unfortunately, at the moment, they fail on Java 17 (EA) with:

java.lang.reflect.InaccessibleObjectException: Unable to make field private static final java.util.HashMap java.lang.ProcessEnvironment.theEnvironment accessible: module java.base does not "opens java.lang" to unnamed module @527b7298

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Michael1993commented, Aug 16, 2021

There is really no nicer way to say this: you should have read the documentation. Here is the relevant section:

Warnings for Reflective Access

As explained above, this extension uses reflective access to change the otherwise immutable environment variables. On Java 9 to 16, this leads to a warning like the following:

[ERROR] WARNING: An illegal reflective access operation has occurred
[ERROR] WARNING: Illegal reflective access by org.junitpioneer.jupiter.EnvironmentVariableUtils [...] to field [...]
[ERROR] WARNING: Please consider reporting this to the maintainers of org.junitpioneer.jupiter.EnvironmentVariableUtils
[ERROR] WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
[ERROR] WARNING: All illegal access operations will be denied in a future release

On Java 17 and later, you get this error instead:

java.lang.reflect.InaccessibleObjectException: Unable to make field [...] accessible:
module java.base does not "opens java.lang" to unnamed module [...]

The best way to prevent these warnings/errors, is to change the code under test, so this extension is no longer needed. The next best thing is to allow access to that specific package with --add-opens=java.base/java.util=ALL-UNNAMED (if you place JUnit Pioneer on the class path) or --add-opens=java.base/java.util=org.junitpioneer (if you place it on the module path).

These command line options need to be added to the JVM that executes the tests:

1reaction
ashleyfriezecommented, Jan 10, 2022

This issue could be solved - see https://github.com/webcompere/system-stubs/blob/main/system-stubs-core/src/main/java/uk/org/webcompere/systemstubs/environment/EnvironmentVariableMocker.java

The solution involves depending on mockito-inline, but is easily adopter. You could reasonably copy the above code, or could add the above as a dependency for JUP.

I’m the author of the above class, so feel free to use it as you wish.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Warning: JAVA_HOME environment variable is not set
Warning: JAVA_HOME environment variable is not set. I already setting the JAVA_HOME's path at C:\Program Files\Java\jdk-19\bin . This is the ...
Read more >
How to solve the error "invalid target release: 17"
To set the JAVA_HOME environment variable in Windows, follow these steps: Press the Windows key + R to open the “Run” dialog. In...
Read more >
JDK 17 Releases - Java (JDK)
JDK 17 has been superseded. Please visit jdk.java.net for the current version. Older releases, which do not include the most up to date...
Read more >
"The JDK_HOME environment variable is not set for ... - ERROR
This error comes when the environment variable JDK_HOME is not set in the agent selected in the Runtime Environment to compile the Java...
Read more >
Update Java runtime settings on Windows
In the Java Control Panel, click on the Java tab. Click View to display the Java Runtime Environment Settings; Verify that the latest...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found