0

Ok, I have App.jar as a runnable jar of my applicaiton. It is dependent on many more jars, defining the functionality of the applicaiton (such as worldwind.jar providing 3D globe). One way how to make it runnable is to provide all the jars in one folder set the Class-Path of MANIFEST.MF as :

Class-Path: .

I would, however prefer, to merge all jars into one. Is that possible?

MichalB
  • 3,281
  • 6
  • 32
  • 46
  • If it doesn't entail too much work, you might considered using Maven to build your project. I believe that it will handle all this for you. Otherwise, I've always found this to be a long and error-prone process. – DaveH May 30 '13 at 12:37

3 Answers3

1

Raw answer: as jar es basically a zip file, just open all the jars you want and copy the classes in the destination jar. However, today we usually let tools like maven to take care of dependencies

fGo
  • 1,146
  • 5
  • 11
0

Yes it is.

I have a quick example here with Ant, if you're using NetBeans: in the build.xml of your project, add this target and modify the naming as you wish.

This will merge all jars in your "dist/lib" folder with the output jar of your project and put them in the "merged" folder.

You can easily adapt this to suit your need.

Then you only need to right-click the build.xml file, "run target", "other targets", click "merged".

    <target name="merged" depends="jar">
        <property name="name" value="yourMergedJar"/>
        <property name="dir" value="merged"/>
        <property name="jar" value="${dir}/${name}.jar"/>
        <echo message="Packaging ${application.title} into a single JAR at ${jar}"/>
        <delete dir="${dir}"/>
        <mkdir dir="${dir}"/>
        <jar destfile="${dir}/temp.jar" filesetmanifest="skip">
            <zipgroupfileset dir="dist" includes="*.jar"/>
            <zipgroupfileset dir="dist/lib" includes="*.jar"/>
            <manifest>
                <attribute name="Main-Class" value="${main.class}"/>
            </manifest>
        </jar>
        <zip destfile="${jar}">
            <zipfileset src="${dir}/temp.jar"
            excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
        </zip>
        <delete file="${dir}/temp.jar"/>
    </target>

Edit

As other members say, you can do this in any IDE (with Ant or Maven) or even manually, as jars are basically zip files.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • I am using Eclipse and there is also an option to export with a use of Ant script. So it this script XML file (script.xml)? – MichalB May 30 '13 at 12:50
  • Not familiar with Eclipse against Ant, but you can get started here: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-81_basics.htm – Mena May 30 '13 at 12:57
0

Yes. I use the Maven shade plugin for exactly that.

mvn clean package shade:shade
tbsalling
  • 4,477
  • 4
  • 30
  • 51