zoukankan      html  css  js  c++  java
  • Intellij打包jar文件,“java.lang.SecurityException: Invalid signature file digest for Manifest main attrib

    下面是使用Intellij 打包jar文件的步骤,之后会有运行jar文件时遇到的错误。

     

     

    打包完成。

    ==========================================================================

    运行jar出现问题:

    1、找不到主类。打开jar文件包,在MANIFEST.MF文件中添加Main-Class:  包名.类名,

    注意:包名前面有空格,类名没有.java或者.class后缀,最后一定要回车到下一行。让光标定位在空白行。

    打开

    2、java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

    打开META-INF目录,将*.SF,*.DSA,*.RSA文件删除,即可。应为有些包有签名,导致错误。(2019-12-15亲测可行)

    此问题,可以参考下面的连接,这位大神比较详细,http://www.cnblogs.com/fuxinci/p/3356087.html,(如有侵权请告知,会删除,谢谢!)。

     

    使用meaven打包过程中遇到的一些问题

     

    开始使用如下代码进行打包

    复制代码
    复制代码
    <build>
            <!-- mvn assembly:assembly -Dmaven.test.skip=true -->
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.2-beta-5</version>
                    <configuration>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>com.fxc.rpc.impl.member.MemberProvider</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>assembly</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    复制代码
    复制代码

     结果出现spring命名空间无法找到的错误,

    org.xml.sax.SAXParseException: schema_reference.4: 无法读取方案文档 'http://www.springframework.org/schema/beans/spring-beans.xsd', 原因为 1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是 <xsd:schema>。

    据查是由于spring-core,spring-aop每一个jar中都包含了一套spring.handlers,spring.schemas文件,以至于在打包过程中发生了覆盖,网上没有搜到使用maven-assembly-plugin插件如何解决此问题,大多数人建议使用maven-shade-plugin插件,修改后pom代码如下

    复制代码
    复制代码
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.4</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <transformers>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                            <resource>META-INF/spring.handlers</resource>
                        </transformer>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>com.fxc.rpc.impl.member.MemberProvider</mainClass>
                        </transformer>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                            <resource>META-INF/spring.schemas</resource>
                        </transformer>
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin> 
    复制代码
    复制代码

    再次打包,出现文件签名不合法的问题

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

    再查,原来是由于某些包的重复引用,以至于打包之后的META-INF的目录下多出了一些*.SF,*.DSA,*.RSA文件所致(据说解压jar包,然后删掉这些文件再次打包错误就会消失,未确认),再次修改pom.xml,最终使用如下配置文件,运行

    mvn clean install -Dmaven.test.skip=true

    打包成功

    复制代码
    复制代码
    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.4</version>
                <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>
    
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.fxc.rpc.impl.member.MemberProvider</mainClass>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    复制代码
    复制代码

    此时查看target目录下会发现xxx.jar 和original-xxx.jar,后一个不包含引用的jar包,直接运行前一个即可

    java -jar target/xxx.jar 

    成功!

    PS:项目中使用了几个公司自己的jar,在公有库里没有,在eclipse里运行的时候我都是修改scope为system,调用的本地jar,但是在打包的过程中scope=system的jar是不会自己打进去的,很是让我郁闷,我只好讲这些jar安装进入本地资源库

    mvn install:install-file -Dfile=my-jar.jar -DgroupId=org.richard -DartifactId=my-jar -Dversion=1.0 -Dpackaging=jar
  • 相关阅读:
    【Luogu1095】守望者的逃离
    python基础学习1-类相关内置函数
    python基础学习1-面向对象
    python基础学习1 -异常捕获
    python基础学习1-类,对象
    python基础学习1-正则表达式
    python基础学习1-反射
    python基础学习1-日志信息
    python基础学习1-生成器,递归函数
    python基础学习1-json,pickle的序列化和反序列化
  • 原文地址:https://www.cnblogs.com/guohu/p/12044413.html
Copyright © 2011-2022 走看看