5

I have a Java project with embedded jetty server and jersey library for REST Services. I am using Intellij for running the project and its working. The problem is when I try to execute the generated jar file.

So I have written a task for generating jar file in the build.gradle file. Below is the task in the build.gradle

task fatJar(type: Jar) {
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': 1.0
        attributes 'Main-Class': 'EmbeddedJetty.EmbeddedJettyMain'
    }
}

The jar file is generated when I run the below command

gradle clean build fatJar

I am facing the below errors when I try to the generated jar file using the command

java -jar projectname.rar

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
    at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:284)
    at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:238)
    at java.util.jar.JarVerifier.processEntry(JarVerifier.java:273)
    at java.util.jar.JarVerifier.update(JarVerifier.java:228)
    at java.util.jar.JarFile.initializeVerifier(JarFile.java:383)
    at java.util.jar.JarFile.getInputStream(JarFile.java:450)
    at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:940)
    at sun.misc.Resource.cachedInputStream(Resource.java:77)
    at sun.misc.Resource.getByteBuffer(Resource.java:160)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:454)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)

Can someone provide your feedback on what might be the issue?

RaGe
  • 22,696
  • 11
  • 72
  • 104
Maverick
  • 708
  • 9
  • 19

2 Answers2

8

This is likely the result of bundling third party library jars that have been signed to prevent tampering. Your bundling is likely including signature files from these jars in your 'fat' jars manifest. You can explicitly exclude signature files using a directive like:

exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'

related question: "Invalid signature file" when attempting to run a .jar

Community
  • 1
  • 1
dkichler
  • 307
  • 1
  • 11
  • 1
    Where add to add this line ? In build.gradle ? I am getting syntax error on adding this to build.gradle, could you paste a sample build.gradle with this line. – Arjun Bora May 05 '17 at 06:33
  • Arjun, the exclude directive needs to be added to the JAR plugin configuration in your build.gradle file. See the [JAR plugin documentation](https://docs.gradle.org/3.5/dsl/org.gradle.api.tasks.bundling.Jar.html) on the exclude(s) property/methods. Also, [this answer](http://stackoverflow.com/a/14441628/1772794) includes a simple example. – dkichler May 10 '17 at 18:43
0

You said you generated jar file and you are trying to run a .rar file using java -jar.

Nevin
  • 769
  • 1
  • 11
  • 26