Tag Archives: “java.lang.SecurityException: Invalid signature file digest for Manifest main attrib

Intellij Pack the jar file error: “java.lang.SecurityException: Invalid signature file digest for Manifest main attrib

Here are the steps to use IntelliJ to package jar files, and then there will be errors encountered when running jar files

Packaging is complete

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

Problems in running jar:

1. Main class not found. Open jar package, add main class: package name. Class name in manifest.mf file

Note: there is a space in front of the package name, and the class name has no. Java or. Class suffix. Finally, be sure to return to the next line. Position the cursor on a blank line

Open

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

Open meta-inf directory and delete *. SF, *. DSA, *. RSA files. Some packages should be signed, causing an error (parent test is feasible on December 15, 2019)

Some problems encountered in packaging with meaven

Start packaging with the following code

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

As a result, an error occurred that the spring namespace could not be found

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

According to the investigation, each jar of spring core and spring AOP contains a set of spring.handlers and spring.schemas files, so that they are covered in the packaging process. It is not found on the Internet how to solve this problem by using the Maven assembly plugin plug-in. Most people suggest using the Maven shade plugin plug-in. After modification, the POM code is as follows

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

 

Package again, the file signature is illegal

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

Check again, it turns out that due to the repeated references of some packages, there are more *. SF, *. DSA, *. RSA files in the directory of meta-inf after packaging (it is said that decompressing the jar package, deleting these files, and packaging again, the error will disappear, unconfirmed). Modify pom.xml again, and finally use the following configuration file to run

mvn clean install -Dmaven.test.skip=true

Packaged successfully

 

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

 

At this point, if you look in the target directory, you will find xxx.jar and original-xxx.jar. The latter does not contain the referenced jar package. Just run the former directly

java -jar target/xxx.jar

Success

PS: several companies’ own jars are used in the project, but they are not in the public library. When running in eclipse, I always modify the scope to system and call the local jars. However, during the packaging process, the jars of scope = system will not be entered by myself, which makes me very depressed. I have to say that these jars are installed in the local resource library

mvn install:install-file -Dfile=my-jar.jar -DgroupId=org.richard -DartifactId=my-jar -Dversion=1.0 -Dpackaging=jar