How to Add a Jar in Netbeans

How to add a JAR in NetBeans

You want to add libraries to your project and in doing so you have two options as you yourself identified:

Compile-time libraries are libraries which is needed to compile your application. They are not included when your application is assembled (e.g., into a war-file). Libraries of this kind must be provided by the container running your project.

This is useful in situation when
you want to vary API and implementation, or when the library is supplied by the container (which is typically the case with javax.servlet which is required to compile but provided by the application server, e.g., Apache Tomcat).

Run-time libraries are libraries which is needed both for compilation and when running your project. This is probably what you want in most cases. If for instance your project is packaged into a war/ear, then these libraries will be included in the package.

As for the other alernatives you have either global libraries using Library Manageror jdk libraries. The latter is simply your regular java libraries, while the former is just a way for your to store a set of libraries under a common name. For all your future projects, instead of manually assigning the libraries you can simply select to import them from your Library Manager.

Adding external JAR to Maven project in NetBeans

You can follow this tutorial:
http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

Example:

Install the jar to your local maven repository:

mvn install:install-file -Dfile=cxf-2.7.3.jar -DgroupId=org.apache.cxf -DartifactId=cxf-bundle -Dversion=2.7.3 -Dpackaging=jar

Edit the pom.xml file in your project to include the newly added dependency:

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle</artifactId>
<version>2.7.3</version>
</dependency>

This should work regardless of the IDE you are using.

How can I include external jar on my Netbeans project

If you copy your jars into the source code directory, they will be in your final jar. Nevetheless, I am not sure if this will work 100% of the time.

There is a great post at java-forum that states the following:

Except for a select few circumstances, what works best for me is to
simply merge the files manually. A .jar is basically a .zip with
organized contents, and you can open them in almost any .zip capable
archive program (I just use gnome's standard archiver, File Roller,
and it works great). Backup your jar file and open it in the archiver
of your choice, and do the same for each library jar in the library
directory. Drag and drop the working folders (IE, everything EXCEPT
the META-INF Directory) from each library into your jar's root path
(alongside your META-INF and your app's root package). Now drag the
META-INF/MANIFEST.MF file from your jar to your Desktop or any other
folder. Open it, and erase the Class-Path and X-COMMENT lines. Don't
forget to leave a blank newline at the end of the file! Save the new
manifest file and drag it back to your jar's META-INF directory,
overwriting the old one. Test the jar.

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

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>

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

OK, found the answer at the following site : http://arunasujith.blogspot.com/2011/08/how-to-build-fat-jar-using-netbeans.html

Can't add jar to palette in NetBeans

NetBeans is seems to bugging out here but I've found a work around which consists in importing the jar file as a library into the project.



Related Topics



Leave a reply



Submit