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

  7. 22/10/2018 at 04:57

    Very good info. Lucky me I discovered your site by accident (stumbleupon).
    I have book marked it for later!

  8. 01/12/2018 at 07:20

    I lie tһe heelpful information you provide in your articles.
    I’ll bookmark уour blog and check aցain heгe frequently.
    Ӏ’m գuite certаіn I will learn plenty of new stuff rіght heге!
    Good luck for the next!

  1. 05/04/2019 at 16:51
  2. 12/09/2019 at 00:23
  3. 19/10/2019 at 00:45

Leave a reply to Banoks Software Solution Cancel reply