“Invalid signature file” when attempting to run a .jar

后端 未结 21 2020
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 05:34

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:

相关标签:
21条回答
  • 2020-11-22 06:16

    In case you're using gradle, here is a full farJar task:

    version = '1.0'
    //create a single Jar with all dependencies
    task fatJar(type: Jar) {
        manifest {
            attributes 'Implementation-Title': 'Gradle Jar File Example',  
                'Implementation-Version': version,
                'Main-Class': 'com.example.main'
        }
        baseName = project.name + '-all'
        from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
        exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA' 
        with jar
    }
    
    0 讨论(0)
  • 2020-11-22 06:16

    For those who have trouble with the accepted solution, there is another way to exclude resource from shaded jar with DontIncludeResourceTransformer:

    https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#DontIncludeResourceTransformer

              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                    <resource>BC1024KE.DSA</resource>
                </transformer>
              </transformers>
    

    From Shade 3.0, this transformer accepts a list of resources. Before that you just need to use multiple transformer each with one resource.

    0 讨论(0)
  • 2020-11-22 06:17

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

    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/

    0 讨论(0)
  • 2020-11-22 06:18

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

    jar {
        doFirst {
            from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } 
        }
        exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA' 
    }
    
    0 讨论(0)
  • 2020-11-22 06:20

    Please use the following command

    zip -d yourjar.jar 'META-INF/*.SF' 'META-INF/*.RSA' 'META-INF/*SF'
    
    0 讨论(0)
  • 2020-11-22 06:22

    I faced the same issue, after reference somewhere, it worked as below changing:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
            <createDependencyReducedPom>false</createDependencyReducedPom>
        </configuration>
        <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>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
    0 讨论(0)
提交回复
热议问题