How to add a JAR in NetBeans

JavaNetbeans

Java Problem Overview


Let's say you create a new project, and want it to make use of some 3rd party library, say, widget.jar. Where do you add this JAR:

  1. File >> Project Properties >> Libraries >> Compile-Time Libraries; or
  2. File >> Project Properties >> Libraries >> Run-Time Libraries; or
  3. Tools >> Libraries (Library Manager) >> Library Classpath; or
  4. Tools >> Java Platforms (Java Platform Manager)

All of these dialogs seem to do the same thing but I'm sure they all have their proper usages. Can't find a good "best practices" article online and the NetBeans Help Contents dialog isn't helping with this either.

Java Solutions


Solution 1 - Java

Right click 'libraries' in the project list, then click add.

Solution 2 - Java

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.

Solution 3 - Java

If your project's source code has import statements that reference classes that are in widget.jar, you should add the jar to your projects Compile-time Libraries. (The jar widget.jar will automatically be added to your project's Run-time Libraries). That corresponds to (1).

If your source code has imports for classes in some other jar and the source code for those classes has import statements that reference classes in widget.jar, you should add widget.jar to the Run-time libraries list. That corresponds to (2).

You can add the jars directly to the Libraries list in the project properties. You can also create a Library that contains the jar file and then include that Library in the Compile-time or Run-time Libraries list.

If you create a NetBeans Library for widget.jar, you can also associate source code for the jar's content and Javadoc for the APIs defined in widget.jar. This additional information about widget.jar will be used by NetBeans as you debug code. It will also be used to provide addition information when you use code completion in the editor.

You should avoid using Tools >> Java Platform to add a jar to a project. That dialog allows you to modify the classpath that is used to compile and run all projects that use the Java Platform that you create. That may be useful at times but hides your project's dependency on widget.jar almost completely.

Solution 4 - Java

Project Files Services Tabls

go files tabs

drag drop file to libs files hover.

return project tabs and what are you see :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPamView Question on Stackoverflow
Solution 1 - JavaPetahView Answer on Stackoverflow
Solution 2 - JavaJohan SjöbergView Answer on Stackoverflow
Solution 3 - JavavkraemerView Answer on Stackoverflow
Solution 4 - JavaAbdullah SARGINView Answer on Stackoverflow