我的java程序被打包在一个jar文件中,并使用了一个外部jar库,bouncy castle。我的代码编译良好,但运行jar会导致以下错误:
java.lang.SecurityException: Manifest主属性的签名文件摘要无效
我在谷歌上搜索了一个多小时,想要找到一个解释,但几乎没有什么价值。如果有人看到这个错误之前,可以提供一些帮助,我将不胜感激。
我的java程序被打包在一个jar文件中,并使用了一个外部jar库,bouncy castle。我的代码编译良好,但运行jar会导致以下错误:
java.lang.SecurityException: Manifest主属性的签名文件摘要无效
我在谷歌上搜索了一个多小时,想要找到一个解释,但几乎没有什么价值。如果有人看到这个错误之前,可以提供一些帮助,我将不胜感激。
当前回答
对于那些在尝试使用maven-shade-plugin创建阴影超级jar时遇到此错误的人,解决方案是通过在插件配置中添加以下行来排除清单签名文件:
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!-- Additional configuration. -->
</configuration>
其他回答
这里列出的解决方案可能提供一个指针。
清单主属性的签名文件摘要无效
底线:
最好把官方的罐子保存为 只是将其作为依赖项添加到您的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)
}
有可能是两个不同的签名者搞砸了java思维。
尝试从jar中删除META-INF文件夹,再次添加清单和签名jar,它帮助了我:http://jehy.ru/articles/2013/12/13/invalid-signature-file-digest-for-manifest-main-attributes/
这发生在我在Intellij中,当我点击“添加为Maven项目”底线时,Intellij说“发现非托管pom文件”。同时,out文件夹已经生成。所以它最近没有变化。
删除文件夹和运行程序解决了我的问题。然后重新创建Out文件夹。
看看小狐狸的回答吧。我收到的错误和他的非常相似。
一种策略包括使用ANT来简化从每个Jar文件中删除签名。它将采取下列步骤:
复制舱单。MF在临时文件中 从临时文件中删除Name和SHA条目 用临时清单创建一个临时Jar文件 移除临时舱单 将原始Jar文件与临时Jar文件交换
下面是一个ANT宏def做的工作:
<macrodef name="unsignjar" description="To unsign a specific Jar file">
<attribute name="jarfile"
description="The jar file to unsign" />
<sequential>
<!-- Copying to the temporary manifest file -->
<copy toFile="@{jarFile}_MANIFEST.tmp">
<resources>
<zipentry zipfile="@{jarFile}" name="META-INF/MANIFEST.MF"/>
</resources>
</copy>
<!-- Removing the Name and SHA entries from the temporary file -->
<replaceregexp file="@{jarFile}_MANIFEST.tmp" match="\nName:(.+?)\nSH" replace="SH" flags="gis" byline="false"/>
<replaceregexp file="@{jarFile}_MANIFEST.tmp" match="SHA(.*)" replace="" flags="gis" byline="false"/>
<!-- Creating a temporary Jar file with the temporary manifest -->
<jar jarfile="@{jarFile}.tmp"
manifest="@{jarFile}_MANIFEST.tmp">
<zipfileset src="@{jarFile}">
<include name="**"/>
<exclude name="META-INF/*.SF"/>
<exclude name="META-INF/*.DSA"/>
<exclude name="META-INF/*.RSA"/>
</zipfileset>
</jar>
<!-- Removing the temporary manifest -->
<delete file="@{jarFile}_MANIFEST.tmp" />
<!-- Swapping the original Jar file with the temporary one -->
<move file="@{jarFile}.tmp"
tofile="@{jarFile}"
overwrite="true" />
</sequential>
`
在ANT任务中可以这样调用定义:
<target name="unsignJar">
<unsignjar jarFile="org.test.myjartounsign.jar" />
</target>