How to compile JAVA Project into a single JAR File using NetBeans

I found an interesting article about compiling a NetBeans project into a single JAR file.

<?xml version="1.0" encoding="UTF-8"?>
<project name="build-OneJar1" default="all" basedir="..">
   <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="" value="MyJarName"/>
        <property name="" value="MyProject"/>

        <!– don’t edit below this line –>

        <property name="store.dir" value="store"/>
        <property name="store.jar" value="${store.dir}/${}.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"/>

                <attribute name="Main-Class" value="${main.class}"/>

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

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

  • Save the above code as build-OneJar.xml and save it in the NetBeans project folder.( I saved in the nbproject folder and also had the file as a ANT project file.).

  • In Build.xml put an import statement and import the build-onejar.xml file.

  • To build the NetBeans project into a single JAR file, it needs to be run using the specified parameters as TARGET. To do this Right Click build.xml in the Files TAB and select Run Target –> Other Targets –> Package-For-Store.

  • The JAR file will be built and stored in a folder called Store in the
  • Also remember to save the build-onejar.xml file as a template for future use. To do this go right click on the file in the Files TAB and select Save As Template….. select the appropriate folder and click OK.


  In this way a NetBeans project can be compiled into a single JAR file.



One Response

  1. With Maven there’s an assembly plugin to do that.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: