Setting environment fails on Java 17-ea
See original GitHub issueI 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:
- Created 2 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
On Java 17 and later, you get this error instead:
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:
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.