Java – How can I use JDK11 to prevent java.lang.IllegalAccessError?

How can I use JDK11 to prevent java.lang.IllegalAccessError?… here is a solution to the problem.

How can I use JDK11 to prevent java.lang.IllegalAccessError?

We received the following exception in our test system. Know how we can use command-line switches to prevent this exception. Any idea how this anomaly occurs?

Is it possible to disable full access checking in a test system that uses reflection and class loading intensively?

java.lang.IllegalAccessError: class java.io.File (in module java.base) cannot access class javax.print.PrintException (in module java.desktop) because module java.base does not read module java.desktop
    at java.io.File.exists(File.java)
    at jdk.internal.loader.URLClassPath$FileLoader.getResource(URLClassPath.java:1199)
    at jdk.internal.loader.URLClassPath.getResource(URLClassPath.java:314)
    at jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:697)
    at jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)
    at jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:398)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:67)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
    at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:66)

Solution

Try running it by adding the following command parameters.

--add-reads java.base=java.desktop

Related Problems and Solutions