Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 947
mjm Avatar asked Oct 29 '22 11:10

mjm


1 Answers

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.

like image 113
Vampire Avatar answered Nov 16 '22 12:11

Vampire