Java 尝试运行 .jar 时出现“无效签名文件”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/999489/
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-08-11 22:05:22  来源:igfitidea点击:

"Invalid signature file" when attempting to run a .jar

javajarexecutable-jar

提问by

My java program is packaged in a jar file and makes use of an external jar library, bouncy castle. My code compiles fine, but running the jar leads to the following error:

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

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

线程“main”中的异常 java.lang.SecurityException:Manifest 主要属性的签名文件摘要无效

I've googled for over an hour searching for an explanation and found very little of value. If anyone has seen this error before and could offer some help, I would be obliged.

我用谷歌搜索了一个多小时寻找解释,但发现价值很小。如果有人以前见过这个错误并且可以提供一些帮助,我将不胜感激。

采纳答案by Nrj

The solution listed here might provide a pointer.

此处列出的解决方案可能会提供一个指针。

Invalid signature file digest for Manifest main attributes

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

Bottom line :

底线:

It's probably best to keep the official jar as is and just add it as a dependency in the manifest file for your application jar file.

最好保持官方 jar 原样,并将其添加为应用程序 jar 文件的清单文件中的依赖项。

回答by 999michal

Compare the folder META-INF in new jar with old jar (before you added new libraries). It is possibility that there will be new files. If yes, you can remove them. It should helps. Regards, 999michal

将新 jar 中的文件夹 META-INF 与旧 jar(在添加新库之前)进行比较。有可能会有新文件。如果是,您可以删除它们。它应该有帮助。问候, 999michal

回答by Jus12

I had a similar problem. The reason was that I was compiling using a JDK with a different JRE than the default one in my Windows box.

我有一个类似的问题。原因是我使用 JDK 进行编译,而 JDK 的 JRE 与 Windows 框中的默认 JRE 不同。

Using the correct java.exe solved my problem.

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

回答by Kim Stebel

Assuming you build your jar file with ant, you can just instruct ant to leave out the META-INF dir. This is a simplified version of my ant target:

假设您使用 ant 构建 jar 文件,您可以指示 ant 忽略 META-INF 目录。这是我的蚂蚁目标的简化版本:

<jar destfile="app.jar" basedir="${classes.dir}">
    <zipfileset excludes="META-INF/**/*" src="${lib.dir}/bcprov-jdk16-145.jar"></zipfileset>
    <manifest>
        <attribute name="Main-Class" value="app.Main"/>
    </manifest>
</jar>

回答by ruhsuzbaykus

For those who got this error when trying to create an uber-jarwith maven-shade-plugin, the solution is to exclude manifest signature files by adding the following lines to the plugin configuration:

对于那些谁得到了试图创建一个当这个错误尤伯杯罐子maven-shade-plugin,解决的办法是通过添加以下行插件配置排除清单签名文件:

<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>

回答by Rich Apodaca

Some of your dependencies are likely signed jarfiles. When you combine them all into one big jarfile, the corresponding signature files are still present, and no longer match the "big combined" jarfile, so the runtime halts thinking the jar file has been tampered with (which it...has so to speak).

您的某些依赖项可能是已签名的 jarfile。当您将它们全部组合成一个大 jarfile 时,相应的签名文件仍然存在,并且不再与“大组合”jarfile 匹配,因此运行时停止认为 jar 文件已被篡改(它...说话)。

You can solve the problem by eliminating the signature files from your jarfile dependencies. Unfortunately, it's not possible to do this in one step in ant.

您可以通过从 jarfile 依赖项中消除签名文件来解决该问题。不幸的是,在 ant 中不可能一步完成

However, I was able to get this working with Ant in two steps, without specifically naming each jarfile dependency, by using:

但是,我可以通过使用以下两个步骤来使用 Ant,而无需专门命名每个 jarfile 依赖项:

<target name="jar" depends="compile" description="Create one big jarfile.">
    <jar jarfile="${output.dir}/deps.jar">
        <zipgroupfileset dir="jars">
            <include name="**/*.jar" />
        </zipgroupfileset>
    </jar>
    <sleep seconds="1" />
    <jar jarfile="${output.dir}/myjar.jar" basedir="${classes.dir}">
        <zipfileset src="${output.dir}/deps.jar" excludes="META-INF/*.SF" />
        <manifest>
            <attribute name="Main-Class" value="com.mycompany.MyMain" />
        </manifest>
    </jar>
</target>

The sleep element is supposed to prevent errors about files with modification dates in the future.

sleep 元素应该防止将来修改日期的文件出错

Other variations I found in the linked threads didn't work for me.

我在链接线程中发现的其他变体对我不起作用。

回答by Keith P

For those using gradle and trying to create and use a fat jar, the following syntax might help.

对于那些使用 gradle 并尝试创建和使用胖 jar 的人,以下语法可能会有所帮助。

jar {
    doFirst {
        from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } 
    }
    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA' 
}

回答by Jehy

It's possible that two different signers mess up java mind.

两个不同的签名者可能会混淆 Java 思维。

Try removing META-INF folder from jar, adding manifest and signing JAR again, it helped me: http://jehy.ru/articles/2013/12/13/invalid-signature-file-digest-for-manifest-main-attributes/

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

回答by Dean Wild

If you're getting this when trying to bind JAR files for a Xamarin.Android bindings project like so:

如果您在尝试为 Xamarin.Android 绑定项目绑定 JAR 文件时遇到此问题,如下所示:

JARTOXML : warning J2XA006: missing class error was raised while reflecting com.your.class : Invalid signature file digest for Manifest main attributes

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

Just open the JAR files using Winzip and delete the meta-inf directories. Rebuild - job done

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

回答by Travis

I had this problem when using IntelliJ IDEA 14.01.

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

I was able to fix it by:

我能够通过以下方式修复它:

File->Project Structure->Add New (Artifacts)->jar->From Modules With Dependencies on the Create Jar From Module Window:

File->Project Structure->Add New (Artifacts)->jar->From Modules with Dependencies 在 Create Jar From Module 窗口:

Select you main class

选择你的主班

JAR File from Libraries Select copy to the output directory and link via manifest

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