4

I have some jar files into /libs/ and my project classes are located at /target/. To remove dependency on external folder. I want to make a consolidate jar file. Which contains all the classes from jar files found in all /libs/*.jar

I am using following Ant script:

<jar destfile="mytest.jar" basedir="target/classes">
    <manifest>
        <attribute name="Main-Class" value="com.mytest.MyProcessor"/>
    </manifest>

    <fileset dir="target/classes" includes="**/*.class"/>
    <zipgroupfileset dir="libs" includes="*.jar"/>
</jar>

But, when I run the jar using "java -jar mytest.jar" command, it says:

Exception in thread "main" java.lang.SecurityException: no manifiest section for signature file entry javax/mail/internet/ContentDisposition.class at sun.security.util.SignatureFileVerifier.verifySection(Unknown Source) at sun.security.util.SignatureFileVerifier.processImpl(Unknown Source)

Any idea, how to make a consolidated jar that have no such issues, would be highly appreciated. I am using IntelliJ IDEA.

yorkw
  • 40,926
  • 10
  • 117
  • 130
Asif Shahzad
  • 843
  • 2
  • 11
  • 21
  • Sometime it triggers: Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes – Asif Shahzad Mar 06 '12 at 21:28

4 Answers4

9

Exception in thread "main" java.lang.SecurityException: no manifiest section for signature file entry

This error message tells exactly why bundled jar is not recommended for production release. The problem is some of your dependency jar is signed with signature files. repack it may violate some security policy on the dependency jar.

To make it work, you need remove all signature files from the signed jar, before copying the unpacked files into your final build jar file. Note that <zipgroupfileset> doesn't support exclude file within jar archive, try using a list of <zipfileset> instead:

<zipfileset src="libs/mail.jar">
  <exclude name="**/*.RSA, **/*.SF, **/*.DSA"/>
</zipfileset>
... ...
yorkw
  • 40,926
  • 10
  • 117
  • 130
  • I tried your solution, in my case, there are MANY jars, so I use . But it copies all jars as is in resultant jar instead of extracting each classes putting into new jar. Which results into classnotfound error. – Asif Shahzad Mar 07 '12 at 12:39
  • @AsifShahzad, is used for specify single jar archive, doesn't make any sense, classnotfound error is expected since you are not include any of your jar libraries in final jar. If you have many jars and you are sure mail.jar is the only signed one, try using together with a single that specify/process the signed mail.jar separately. – yorkw Mar 07 '12 at 20:06
  • Oh sorry, it was typo, I mean . Any way ... I have done it not exactly as above but in almost same lane. I created consolidated jar without excluding anything, then from it moved all RSA, SF, and DSA files. It works great. Thank you so much. – Asif Shahzad Mar 08 '12 at 06:54
2

Something like this might work for you.

<jar destfile="mytest.jar" basedir="target/classes">
    <restrict>
        <not>
            <or>
                <name name="**/*.RSA"/>
                <name name="**/*.SF"/>
                <name name="**/*.DSA"/>
            </or>
        </not>
        <archives>
            <zips>
                <fileset dir="libs" includes="**/*.jar"/>
            </zips>
        </archives>
    </restrict>
    <manifest>
        <attribute name="Main-Class" value="com.mytest.MyProcessor"/>
    </manifest>
</jar>

I adapted this from the example at http://ant.apache.org/manual/Tasks/jar.html under the "Merging archives" section.

cjaube
  • 173
  • 6
1

Use Maven and the shade plugin. Have a look at http://maven.apache.org/plugins/maven-shade-plugin/

phlogratos
  • 13,234
  • 1
  • 32
  • 37
  • Our build system is Ant based. Maven is a bit complex, in fact I am hands on with Maven, so I want to do it with Ant, if no alternative found, then will consider Maven's plugin. Thank you. – Asif Shahzad Mar 07 '12 at 12:42
  • I've seen this same problem with a jar produced with the shade plugin – Max Nanasy Dec 20 '13 at 01:15
  • 1
    I can confirm that I am getting this error from the maven shade plugin – grinch Aug 07 '14 at 19:08
  • 1
    @grinch maven shade plugin worked for me. You just need to use some exclude filters. Please check this link for details http://www.jamesswafford.com/2012/03/11/java-lang-securityexception-no-manifest-section-for-signature-file-entry/ – Mashrur Oct 26 '15 at 03:01
0

I would suggest to unjar all your jar files into a lib folder and then jar together with your your source code, try it might work for you.

Hafiz
  • 4,921
  • 3
  • 19
  • 28