Generating Single Executable jar file with all dependencies and libraries using Netbeans and Eclipse

Generating Single Jar file with all dependencies in Eclipse is easy. We just need to follow below steps :

  1. Either from the context menu or from the menu bar’s File menu, select Export.
  2. Expand the Java node and select JAR file. Click Next.
  3. In the JAR File Specification page, select the resources that you want to export in the Select the resources to export field.
  4. Select the appropriate checkbox to specify whether you want to Export generated class files and resources or Export Java source files and resources. (Note: Selected resources are exported in both cases.
  5. If there are other files or resources you want to include they must be in a an open project. Browse to their location via the directory tree on the left and ensure the file or resource is checked in the window on the right.
  6. In the Select the export destination field, either type or click Browse to select a location for the JAR file.
  7. Select or clear the Overwrite existing files without warning checkbox. If you clear this checkbox, then you will be prompted to confirm the replacement of each file that will be overwritten.
  8. Click Finish to create the JAR file immediately.
  9. Now, navigate to the location you specified for the jar. The icon you see and the behavior you get if you double click it will vary depending on how your computer is set up.
  10. You can use this article for images and more detail.

How to generate single jar file in NetBeans with all dependency and libraries :

Generating single jar file in eclipse is easy but in NetBeans, libraries and dependencies are not automatically combined in one jar file. However there is one time setup required and generating jar file in NetBeans becomes easier than even Eclipse.

Open “build.xml” file generated by NetBeans project and add below “<target>” tag just before   “</project>” tag.

<target name="-post-jar">
    <property name="store.jar.name" value="AllInOneJar"/>
    <property name="store.dir" value="dist"/>
    <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
    <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
    <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>

Posted

in

by


Related Posts

Comments

8 responses to “Generating Single Executable jar file with all dependencies and libraries using Netbeans and Eclipse”

  1. Bikram Barthakur Avatar
    Bikram Barthakur

    your code saved my day.. I am able to create a single application jar file with all the library jars in it. Thanks and credits to you.

    1. Jitendra Zaa Avatar

      Fantastic news 🙂 Happy coding

  2. Jayanta Pramanik Avatar
    Jayanta Pramanik

    I’m using NetBeans 8.0.2 and it is showing following error after adding target tag just before project tag. tips needed.

    Error : Unexpected element “{}target” {antlib:org.apache.tools.ant}target

    1. Robert N Lockwood Avatar
      Robert N Lockwood

      Netbeans 8.2
      Get an error about duplicate name name=”-post-jar”

  3. argos Avatar

    super Gracias 😀

  4. Kit S Kennedy Avatar
    Kit S Kennedy

    I need help. I tried your solution and it seemed to work, but when I run it I get ClassNotFoundException for a required class. I looked in the AllInOneJar.jar and it is not there, but it is in one of the libraries that is added to the project. Any ideas?

  5. fabiortsf Avatar

    This not work in 2019 NetBeans V11

  6. […] 确保您的JAR文件是可执行的JAR;我不确定如何从NetBeans导出可执行的JAR(我使用Eclipse),但是this文章似乎很好地解释了它。 […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading