0
votes

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

1
Can you clarify your question, by editing it (not by adding comments). What outcome are you trying to achieve? What are you adding to your build.xml? Which ant target are you trying to run?andrewJames

1 Answers

0
votes

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}"
             update="true">
            <restrict> 
                <archives>
                   <zips>
                       <fileset dir="${dist.dir}" includes="lib/*.jar"/>
                   </zips>
                </archives>
            </restrict>
        </jar>
    </target>

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 Files\Java\jdk1.8.0_211\bin\java" -jar C:\tmp\DemoOne.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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.ajames</groupId>
    <artifactId>UberJarExample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.10</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>UberJarExample</finalName>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Built-By>ajames</Built-By>
                                        <Main-Class>org.ajames.uberjar.App</Main-Class>
                                        <Build-Number>123</Build-Number>
                                        <!-- https://planet.jboss.org/post/building_multi_release_jars_with_maven -->
                                        <Multi-Release>false</Multi-Release>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>            

        </plugins>
    </build>

</project>