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

时间:2014-03-21 18:00:15

标签: java maven securityexception

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>

5 个答案:

答案 0 :(得分:9)

上网几个小时后,仍无法找到解决方案,我偶然发现了解决问题的这个命令:

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

此命令会删除先前jar的签名文件,然后一旦签名,您就不会发现错误。

答案 1 :(得分:6)

我想我已经为自己的问题找到了解决方案,下面提到pom.xml的标签解决了这个问题:

<excludes>
    <exclude>META-INF/*.SF</exclude>
    <exclude>META-INF/*.DSA</exclude>
    <exclude>META-INF/*.RSA</exclude>
    <exclude>junit:junit</exclude>
    <exclude>org.apache.maven:lib:tests</exclude>
</excludes>

http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#Signed_JAR_File

答案 2 :(得分:1)

我遇到了类似的问题,我添加了导致此错误的Bounty Castle依赖项。刚删除它,之后就可以了。

答案 3 :(得分:1)

此错误意味着maven shade插件已修改您的依赖项之一,因此这些库的签名无效。

删除签名文件是一种变通方法,并且取决于您的使用情况,是一个解决方案。对我来说,这是一种不好的做法,因此我最终用maven shade插件替换了Apache Maven Assembly Plugin,以创建具有依赖项的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

答案 4 :(得分:0)

以下Stackexchange问​​题和答案对我有用。它使用过滤器代替和artifactSet排除。我已经在上面的答案中尝试了解决方案,但它没有用。

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