Getting cannot access class com.sun.crypto.provider.SunJCE (in module java.base) error during Tomcat startup

Viewed 1160

Currently we are trying to compile code in JDK 8 and run it in OpenJDK 17. But during Tomcat startup I am getting below error :

Caused by: java.lang.IllegalAccessException: class com.test.TestXYZ cannot access class com.sun.crypto.provider.SunJCE (in module java.base) because module java.base does not export com.sun.crypto.provider to unnamed module 
    at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392)
    at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:489)
    at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:128)
    at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:347

This issue is occurring when we are trying to add instance of com.sun.crypto.provider.SunJCE to java.security.Security by calling addProvider() method of java.security.Security class

I tried the the fix mentioned in How to resolve "The type com.sun.crypto.provider.SunJCE is not accessible" with Java11? but it didnt work may be because this is to fix compile time issue. Not sure though.

Could you please help me resolving this issue ?

POM Entry :

<plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.0</version>
          <configuration>
            <compilerVersion>1.8</compilerVersion>
            <source>1.8</source>
            <target>1.8</target>
            <compilerArgs>
                        <arg>--add-exports=java.base/com.sun.crypto.provider=ALL-UNNAMED</arg>
            </compilerArgs>
          </configuration>
        </plugin>

Tomcat Version : 9.0.36

Thank you

0 Answers
Related