How to include jars in lib into project jar file in Netbeans 8.2?

When I build the jar file, it creates a separate folder, lib, but since I can make it combine everything in one file, I saw how to do it here, but in this new version(8.2) it doesn’t work because the site menus have changed and when I add in the build.xml I can’t right click > run target since it “blocks” that element

The goal is to have only one file, for greater convenience and not having to move the folder lib, generated automatically. The project type is Java Application and I haven’t modified anything in build.xml


Option One

Assuming you have: (a) a standard Ant-based project (which it sounds like you have), and (b) you have your extra JAR files in the usual NetBeans “Libraries” folder, then you can do the following:

1) Edit the project’s build.xml file.

2) Go to the end of the file, and add the following immediately before the final </project> tag:

    <target name="-post-jar">
        <jar destfile="${dist.jar}"
                       <fileset dir="${dist.dir}" includes="lib/*.jar"/>

This adds an extra step to the standard “clean and build” process in NetBeans.

It copies the contents of library files into your application’s JAR file. Note that I explicitly say “the contents” here. It does not copy the JAR files themselves – because you would have to handle the classpath issues arising from that approach (see option 2). It just copies all of the class files from your library JARs into your application’s JAR.

The above is based on the examples found here in the Ant documentation. See the “Merging archives” examples.

Then, you can run the JAR file with something like this:

"C:Program FilesJavajdk1.8.0_211binjava" -jar C:tmpDemoOne.jar

Option Two

Use Maven, not Ant, for your build process (and JAR dependency management). The specifics are outside the scope of this question, but look for the Maven Shade plug-in.

That plug-in was designed specifically to handle the situation in your question – and to deal with more complex situations which may not be handled by my option 1 above. Using Maven to manage your dependent JARs can also make your life much, much easier.

Other Notes

It may feel inconvenient (i.e. less portable) to have a separate “lib” directory containing required library JARs, but it does make sense from the point of view of keeping your code separated (in its own JAR) from other libraries and dependencies.

NetBeans 8.2 was released in October 2016 – it is (by the standards of IDEs) quite old. You may want to consider upgrading to a later version, if that is an option. See here for a list of recent releases.

With a more recent version of NetBeans you will more easily be able to take advantage of newer versions of Java.

Edit – Maven Shade

Here is a Maven-based example (using Java 11), in case it encourages you to take another look at Maven.

This is the Java program:

package org.ajames.uberjar;

import org.apache.commons.lang3.CharUtils;

public class App {

    public static void main(String[] args) {
        System.out.println("The result is " + CharUtils.isAscii('x') + "!");


As you can see, it is very simple. It requires one external JAR file: commons-lang3.

A Maven POM file (pom.xml) is created for you by NetBeans when you choose to create a new Maven-based Java project.

You have to edit that POM file. Here is mine, which creates a single executable JAR containing everything I need:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">


                                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <!-- -->



Leave a Reply

Your email address will not be published. Required fields are marked *