mjm
mjm

Reputation: 723

Error including bouncycastle provider

I need to use bouncycastle provider library in my project.

I have included it the gradle project.

apply plugin: 'application'
sourceCompatibility = '1.6'
version = '1.0.0'
mainClassName = 'path.to.main.file'

    dependencies {
        compile "org.mariadb.jdbc:mariadb-java-client:+"
        compile "org.bouncycastle:bcprov-jdk16:+"
        compile "commons-codec:commons-codec:+"
        testCompile "junit:junit:+"
    }

The project build successfully. But when I try to run the project. It is not able to find the bouncycastle

Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
    at com.example.Server.main(Server.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 6 more

How can I fix it ?

Upvotes: 5

Views: 29994

Answers (2)

Yash
Yash

Reputation: 9568

I have faced the same issue and got resolved by checking whether the BouncyCastleProvider class is loaded into JVM or not. IF not then only load the BouncyCastleProvider class.

Scenarios where some other applications deployed on the Server has already loaded the BouncyCastleProvider class. When you are deploying the other war file and loading the same class this leads to the following exception:

HTTP Status 500 – Internal Server Error
Type Exception Report

Message org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:432)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause

org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
    org.glassfish.jersey.servlet.internal.ResponseWriter.rethrow(ResponseWriter.java:278)

Problem Occured while exceuting the following code:

static {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}

Solution:

static {
    addBCProvider();
    //Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
public static void addBCProvider() {
    // java.security.NoSuchProviderException: no such provider: BC
    if (Security.getProvider(org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME) == null) {
        System.out.println("JVM Installing BouncyCastle Security Providers to the Runtime");
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    } else {
        System.out.println("JVM Installed with BouncyCastle Security Providers");
    }
}

Upvotes: 0

Vampire
Vampire

Reputation: 38649

You probably don't run your application correctly.
If you just run the created JAR with java -jar foo.jar, you miss all dependencies at runtime.
You have to add those dependencies to your classpath.

You have various ways to do this.
E. g. you can create a fat JAR where all dependencies are repacked into the final JAR with some Gradle plugin (there are several, but I don't like this solution at all, so I cannot recomment one).
Or you can e. g. apply the application plugin, then you can use the run task to correctly run your application and use the distZip task to get a ready-made distribution ZIP with your app, all dependencies and start scripts that correctly set the runtime classpath.
Or you can e. g. manually do it with java -cp foo.jar;other.jar;another.jar your.main.Class.

Upvotes: 1

Related Questions