When developing large applications, it's often necessary to utilize third-party libraries. Sometimes, third-party libraries can be distributed as source code that can be dropped into an application, but more often, they are distributed as a set of .jar
files.
NetBeans comes bundled with several class libraries (such as Hibernate, Spring, and TestNG), but it also allows developers to create their own sets of class libraries that can be easily added to projects.
This recipe shows how to create a new library within NetBeans that can then be subsequently used by NetBeans projects. This recipe does not involve writing any Java code, but describes the procedure of creating a library from existing code that other NetBeans projects can then reference.
This recipe assumes that you have a third-party library which is provided as a set of one or more .jar
files that you wish to use within a NetBeans project.
If you do not have a suitable third-party library, a sample library is provided as a part of the download bundle for this book.
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Click on Tools and then click on Libraries on the NetBeans main menu. The Ant Library Manager dialog is displayed as shown in the following screenshot:
Enter the name of the library to be created into the Library Name field as shown in the following screenshot:
Click on the OK button to create the library.
The library has now been created and given a name as specified in step 3. This name is displayed in the Library Name field. Now, we need to add some content to the library.
Using the Browse JAR/Folder dialog box, select a
.jar
file or folder containing the third-party library content that you wish to add to the NetBeans library.Repeat step 7 for any additional
.jar
files or folders that you wish to add to the library.Click on the OK button to complete creation of the library.
A class library in NetBeans is an easy way to add a set of .jar
files or folders to the classpath. When a class library has been defined as detailed in this recipe, it can be added to a project by right-clicking on the project's Libraries node within the Projects explorer and selecting Add Library.... NetBeans will then append the .jar
files and folders from the library into the project's classpath at both compile time and runtime (if the project is runnable).
In addition to adding class libraries to a project to extend the classpath, it is also possible to reference projects from a NetBeans project. If for example, you have a NetBeans project that creates a .jar
file, this can be added into the classpath of a different NetBeans project by right-clicking on the Libraries node in the Projects explorer and selecting Add Project.... The build artifacts from this selected project will then be added to the classpath of the original project.