java 错误包括 bouncycastle 提供程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42630190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:46:47  来源:igfitidea点击:

Error including bouncycastle provider

javagradlebouncycastle

提问by mjm

I need to use bouncycastle providerlibrary in my project.

我需要bouncycastle provider在我的项目中使用库。

I have included it the gradle project.

我已将其包含在 gradle 项目中。

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

项目构建成功。但是当我尝试运行该项目时。它无法找到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 ?

我该如何解决?

回答by Vampire

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.

您可能没有正确运行您的应用程序。
如果您只是使用 运行创建的 JAR java -jar foo.jar,则会在运行时错过所有依赖项。
您必须将这些依赖项添加到您的类路径中。

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 applicationplugin, then you can use the runtask to correctly run your application and use the distZiptask 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.

您有多种方法可以做到这一点。
例如 您可以创建一个胖 JAR,其中所有依赖项都使用一些 Gradle 插件重新打包到最终 JAR 中(有几个,但我根本不喜欢这个解决方案,所以我不能推荐一个)。
或者,您可以例如应用application插件,然后您可以使用该run任务正确运行您的应用程序,并使用该distZip任务获取带有您的应用程序、所有依赖项和正确设置运行时类路径的启动脚本的现成分发 ZIP。
或者您可以例如手动使用java -cp foo.jar;other.jar;another.jar your.main.Class.

回答by Bruno Grieder

BouncyCastle needs to be registered as a security provider

BouncyCastle 需要注册为安全提供者

Security.addProvider( new BouncyCastleProvider() );