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

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

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


当前回答

您可以使用Shadow生成一个jar。

Shadow是一个Gradle插件,用于将项目的依赖类和资源组合到一个输出Jar中。组合罐通常被称为fat-jar或super - Jar。

Modify build.gradle plugins { ... // ① Add the shadow plugin id "com.github.johnrengelman.shadow" version "5.2.0" } ... // ② Config the shadow jar, its name is baseName-1.0-classifier.jar shadowJar { archiveBaseName.set('baseName') archiveClassifier.set('classifier') archiveVersion.set('1.0') manifest { attributes 'Main-Class': 'Main' } } // ③ Disable the default jar task jar.enabled = false // ④ Execute the shadowJar task when compiling build.dependsOn(shadowJar) Execute the command gradle build, the jar file will be generated: <Project Directory>/build/libs/baseName-1.0-classifier.jar

其他回答

如果您在尝试为Xamarin绑定JAR文件时遇到这种情况。Android绑定项目如下:

J2XA006:在反映清单主属性的com.your.class:无效签名文件摘要时引发了缺少类错误

只需使用Winzip打开JAR文件并删除meta-inf目录。重建-工作完成

这里列出的解决方案可能提供一个指针。

清单主属性的签名文件摘要无效

底线:

最好把官方的罐子保存为 只是将其作为依赖项添加到您的manifest文件中 应用程序jar文件。

对于使用kotlin脚本(kts)作为构建脚本的人:

task("fatJar", type = Jar::class) {
baseName = "${project.name}-fat"
manifest {
    attributes["Implementation-Title"] = "Watcher Jar File"
    attributes["Implementation-Version"] = version
    attributes["Main-Class"] = "MainKt"
}
from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) }) {
    exclude(listOf("META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA"))
}
with(tasks.jar.get() as CopySpec)

}

对于那些对已接受的解决方案有问题的人,有另一种方法可以使用dontincleresourcetransformer从阴影jar中排除资源:

https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#DontIncludeResourceTransformer

          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                <resource>BC1024KE.DSA</resource>
            </transformer>
          </transformers>

从Shade 3.0开始,这个转换器接受一个资源列表。在此之前,您只需要使用多个转换器,每个转换器具有一个资源。

我也遇到过同样的问题,经过参考,它的工作原理如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
    <configuration>
        <createDependencyReducedPom>false</createDependencyReducedPom>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>