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

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

  2. RT
    25/06/2015 at 14:47

    Maybe worth noting that you must edit the “build.xml” file. You never mentioned the name of the file to add this text to.

  3. Banoks Software Solution
    06/11/2015 at 07:27

    Thank You brother it helps me a lot. no more lib folder

  4. shyam
    04/02/2016 at 09:49

    while including lib into jar file some of the jar of the lib folder not included into the packaging of jar file. please help why this is happening and what the solution.

  5. Den
    02/09/2016 at 16:28

    Thank you very much, helped out at the right time!

  6. fds
    28/09/2016 at 15:23

    taskdef class net.sf.launch4j.ant.Launch4jTask cannot be found

  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

%d bloggers like this: