我的java程序被打包在一个jar文件中,并使用了一个外部jar库,bouncy castle。我的代码编译良好,但运行jar会导致以下错误:

java.lang.SecurityException: Manifest主属性的签名文件摘要无效

我在谷歌上搜索了一个多小时,想要找到一个解释,但几乎没有什么价值。如果有人看到这个错误之前,可以提供一些帮助,我将不胜感激。


当前回答

如果您正在寻找一个不解包或修改原始库,而是使用一个特殊的JAR类加载器的Fat JAR解决方案,请参阅我这里的项目。

免责声明:代码不是我写的,只是打包并发布在Maven Central上,并在我的阅读中描述如何使用它。

我个人使用它来创建包含BouncyCastle依赖的可运行的优jar。也许它对你也有用。

其他回答

I've recently started using IntelliJ on my projects. However, some of my colleagues still use Eclipse on the same projects. Today, I've got the very same error after executing the jar-file created by my IntelliJ. While all the solutions in here talking about almost the same thing, none of them worked for me easily (possibly because I don't use ANT, maven build gave me other errors which referred me to http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException, and also I couldn't figure out what are the signed jars by myself!)

最后,这帮助了我

zip -d demoSampler.jar 'META-INF/*.SF' 'META-INF/*.RSA' 'META-INF/*SF'

猜猜我的jar文件里删除了什么!

deleting: META-INF/ECLIPSE_.SF 
deleting: META-INF/ECLIPSE_.RSA

这个问题似乎与一些与eclipse相关的文件有关。

我也遇到过类似的问题。原因是我使用的JDK的JRE与Windows系统中的默认JRE不同。

使用正确的java.exe解决了我的问题。

有可能是两个不同的签名者搞砸了java思维。

尝试从jar中删除META-INF文件夹,再次添加清单和签名jar,它帮助了我:http://jehy.ru/articles/2013/12/13/invalid-signature-file-digest-for-manifest-main-attributes/

我在使用IntelliJ IDEA 14.01时遇到了这个问题。

我可以通过以下方法修复它:

文件->项目结构->添加新的(工件)->jar->从模块与依赖创建jar从模块窗口:

选择你的主要类

来自库的JAR文件 选择复制到输出目录和链接通过清单

如果你正在使用gradle,这里有一个完整的farJar任务:

version = '1.0'
//create a single Jar with all dependencies
task fatJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',  
            'Implementation-Version': version,
            'Main-Class': 'com.example.main'
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA' 
    with jar
}