Java 17 and newer is more strict with prohibiting reflection#

Symptoms#

An exception is thrown at runtime whenever code is trying to “make field <foo.bar> accessible”. Example: java.lang.reflect.InaccessibleObjectException: Unable to make field final java.util.Map java.util.Collections$UnmodifiableMap.m accessible: module java.base does not “opens java.util” to unnamed module @5456afaa

Cause#

Starting with Java 17 and above, accessing fields, methods or modules is handled more strictly than previously, and module restrictions have to be modified manually to allow access to e.g. “unnamed modules” (see error message above).

Affected Software#

  • Java library org.junit.contrib.java.lang.system.EnvironmentVariables, which is used by ACADA:Resource-Manager-Base (and potentially other projects)

Solutions#

There are two possible solutions. The first solution is more of a hack, the second solution is preferred.

Solution 1: Disable module accessibility restrictions in the JVM

A simple solution (workaround) involves the specification of arguments for the JVM as environment variables. If the affected code is running inside or interacts with ACS code, then this environment variable has to be set before starting the respective ACS container. It’s probably best to specify the variable in the ~/.bashrc file. The variable has to look like this:

$ export _JAVA_OPTIONS="--add-opens=java.base/java.util=ALL-UNNAMED"

Note: please make sure you’re not overriding previous content of ${_JAVA_OPTIONS}.

Source: <https://www.java67.com/2023/09/how-to-fix-java-module-error-caused-by.html>.

Solution 2: Stop using affected libraries

The preferred solution is given in <https://stackoverflow.com/questions/8168884/how-to-test-code-dependent-on-environment-variables-using-junit/35393623#35393623>. There it is suggested to use the library com.github.stefanbirkner.systemlambda.SystemLambda from <stefanbirkner/system-lambda> instead of org.junit.contrib.java.lang.system.EnvironmentVariables, because the recommended library doesn’t make use of reflection.