Search code examples
javajava-17

Module java.base does not "opens java.lang" (Java 17.0.4.1)


Here's the error: Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected java.lang.ClassLoader() accessible: module java.base does not "opens java.lang" to unnamed module @75dcbabb

Also, I'm using maven to compile. Thank you for reading this thread! Hope you can help me out with this!


Solution

  • On java 17 there are some higher restrictions for some internal modules.

    I'm not sure if you have this issue on the implementation code or "just" for testing. From my experience this issue is more common for the tests because most mocking frameworks will break the restrictions...

    However you have to give in these parameters

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

    If you need this for unit testing you and using maven then you could use a configuration like this:

        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <argLine>
                            --add-opens=java.base/java.lang=ALL-UNNAMED
                            --add-opens=java.base/java.util=ALL-UNNAMED
                        </argLine>
                    </configuration>
                </plugin>
            </plugins>
        </build>