Home > General > How to build a single JAR file with external libs

How to build a single JAR file with external libs

How to combine external libs (.jar files) in an own single JAR file.

If you build a JAR file with NetBeans and your project includes external (or own) compile – time libraries, the final JAR files requires the lib folder with the external JARs in the same directory. For some reasons it’s necessary to have just a single JAR file which everything includes.

Sun (Oracle) describes at  Use NetBeans IDE 6.7 to Combine JAR Files Into a Single JAR File how to do that.

In a nutshell:

Copy following source code into the <project> tag, right before </project> closing tag.

<target name="package-for-store" depends="jar">

        <!-- Change the value of this property to be the name of your JAR,
             minus the .jar extension. It should not have spaces.
             <property name="store.jar.name" value="MyJarName"/>
        -->
        <property name="store.jar.name" value="MarsRoverViewer"/>

        <!-- don't edit below this line -->

        <property name="store.dir" value="store"/>
        <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>

        <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>

        <delete dir="${store.dir}"/>
        <mkdir dir="${store.dir}"/>

        <jar destfile="${store.dir}/temp_final.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="${store.jar}">
            <zipfileset src="${store.dir}/temp_final.jar"
            excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
        </zip>

        <delete file="${store.dir}/temp_final.jar"/>

    </target>

Edit the attributes name and value.

<property name="store.jar.name" value="MarsRoverViewer"/>

Run the target as package-for-store and in the project folder a “store” folder appears. You can find the final TAR file in this folder now.

Greetz Mavi

About these ads
Categories: General Tags: , ,
  1. j.w.v.
    18/08/2010 at 11:32

    Thank you for your tips. They are very useful!! My interest in IT is growing with the minute and to have such a great channel just enlarges it. I am thinking of starting one myself..I will keep you updated.
    Oh and I heard from a friend that the paste you bought for repairing mobile phone screens, which turns out not to be working, is also a great hairgel. Means you did not buy it completely for nothing! Give it a go I would say!

    greetz,
    jwv

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: