Book Image

NetBeans IDE 8 Cookbook

By : David Salter, Rhawi Dantas
Book Image

NetBeans IDE 8 Cookbook

By: David Salter, Rhawi Dantas

Overview of this book

<p>From the start to the end of a Java project's lifecycle, this book will show you how to perform many key tasks with the NetBeans IDE, uncovering more about mobile, desktop, and enterprise Java along the way.</p> <p>You will start by creating Java projects and learning how to refactor and use NetBeans tools to increase developer efficiency. You will then get a walkthrough of how to create a desktop application before covering JavaFX and mobile applications and how to use external services within them. Having seen how to create many different types of applications, you will then be shown how to test and profile them before storing them in revision control systems such as Git or Subversion. Finally, you will learn how to extend NetBeans itself by adding new features to the IDE.</p>
Table of Contents (19 chapters)
NetBeans IDE 8 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a library


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.

Getting ready

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.

How to do it…

  1. 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:

  2. Click on the New Library... button.

  3. Enter the name of the library to be created into the Library Name field as shown in the following screenshot:

  4. Click on the OK button to create the library.

  5. 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.

  6. Click on the Add JAR/Folder button.

  7. 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.

  8. Repeat step 7 for any additional .jar files or folders that you wish to add to the library.

  9. Click on the OK button to complete creation of the library.

How it works…

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).

There's more...

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.