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

19
Exception in thread "main" java.lang.SecurityException: Invalid signature file d                    igest for Manifest main attributes
        at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVeri                    fier.java:240)
        at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier                    .java:193)
        at java.util.jar.JarVerifier.processEntry(JarVerifier.java:262)
        at java.util.jar.JarVerifier.update(JarVerifier.java:216)
        at java.util.jar.JarFile.initializeVerifier(JarFile.java:341)
        at java.util.jar.JarFile.getInputStream(JarFile.java:406)
        at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:75                    2)
        at sun.misc.Resource.cachedInputStream(Resource.java:77)
        at sun.misc.Resource.getByteBuffer(Resource.java:160)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:436)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)

你好,我遇到了这个错误,想知道是什么原因造成的。这是我的项目pox文件的快照。感谢任何帮助。

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>jsch</artifactId>
        <version>0.1.50</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.29</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.3-1101-jdbc41</version>
    </dependency>
    <dependency>
        <groupId>org.objectweb.joram</groupId>
        <artifactId>jftp</artifactId>
        <version>1.52</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <artifactSet>
                            <excludes>
                                <exclude>junit:junit</exclude>
                                <exclude>org.apache.maven:lib:tests</exclude>
                            </excludes>
                        </artifactSet>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
6个回答

19

在上网搜索了数个小时后,仍然找不到解决办法,我偶然发现了这条命令,它解决了我的问题:

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

这个命令会撕掉先前的jar签名文件,然后你签署后就不会再出现错误了。


3
请执行以下命令以移除指定JAR文件中的特定签名文件:zip -d <myjar.jar> 'META-INF/*.DSA' 'META-INF/*.RSA' 'META-INF/*.SF' - luismartingil
这个解决方案是在Wikitechy.com上提供的,也是对我有效的(使用BouncyCastle的应用程序)。 - Blisterpeanuts

11

我试图使用Maven创建一个Uber JAR,但遇到了这个问题,我按照以下步骤解决了该问题。

这是我如何配置shade插件以将twitter4j包含在我的Uber JAR中。


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
    <execution>
        <phase>package</phase>
        <goals>
            <goal>shade</goal>
        </goals>
        <configuration>

            <filters>
                <filter>
                    <artifact>spark-streaming-twitter_2.10:spark-streaming-twitter_2.10</artifact>
                    <includes>
                        <include>**</include>
                    </includes>
                </filter>
                <filter>
                    <artifact>twitter4j-stream:twitter4j-stream</artifact>
                    <includes>
                        <include>**</include>
                    </includes>
                </filter>
                <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>
希望这有所帮助。

我将最新的Microsoft SQL Server jar包导入到本地Maven仓库中,生成jar时抛出了SecurityException异常。我添加了关于SF、DSA和RSA排除的过滤器,问题得以解决!谢谢。 - joninx

6

3
自我回答并没有对我有太大的帮助,因为它没有说明这些行应该放在哪里。在pom.xml中有很多地方可以放置它们,但大多数都没有效果。我在这里找到了一个解决方案,其中包含更好的描述,并且对我有效:https://dev59.com/9uo6XIcBkEYKwwoYIAkf - JWnxp
3
这应该放在阴影插件的配置部分。 - kervin

2
这个错误意味着maven shade插件修改了你的某些依赖项,因此这些库的签名无效。
删除签名文件是一种解决方法,具体取决于您的用例。对我来说,这是一个不好的做法,所以我最终用Apache Maven Assembly Plugin替换了maven shade插件来创建带有依赖关系的jar,并且似乎可以正常工作。https://maven.apache.org/plugins/maven-assembly-plugin/ 例如:
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>
                                    your.class.with.main.method
                                </mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>

将依赖项包含在您的jar文件中:

mvn package

1

我遇到了类似的问题,我添加了Bounty Castle依赖项,导致出现了这个错误。只需将其删除,然后就可以正常工作。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接