How to resolve "The type com.sun.crypto.provider.SunJCE is not accessible" with Java11?

Viewed 5433

I am trying to migrate my application from Java 8 to Java 11. In of one of my project classes, I have the line Security.addProvider(new com.sun.crypto.provider.SunJCE());. I am getting a compile error for this line. How to resolve this?

1 Answers

You can't access com.sun packages without explicitly setting them on your compile options. After adding --add-exports=java.base/com.sun.crypto.provider=ALL-UNNAMED to compilerArgs it compiles without any error.

Code:

   Security.addProvider(new com.sun.crypto.provider.SunJCE());

Maven:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>

    <properties>
        <java.version>11</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>

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

                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

java.security.Security.getProviders() returns you all the providers including the SunJCE.

   Stream.of(java.security.Security.getProviders()).forEach(System.out::println);

So better option to get the SunJCE instane is java.security.Security.getProvider("SunJCE")

Related