NetBeans IDE 7 Cookbook

By Rhawi Dantas
    Advance your knowledge in tech with a Packt subscription

  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. NetBeans Head First

About this book

Java IDEs have grown bigger and more complicated with time. Some development environments even require the user to spend countless hours searching for more software to bundle with the IDE just to start working. NetBeans abstracts much of the work needed to configure the environment and makes it convenient for Java developers to start coding straight away. With this book in hand you will tap into the endless possibilities of developing modern desktop and web applications in Java.

NetBeans IDE 7 Cookbook is perfect for you if you are ready to take the next step from the standard tutorials and move into the practical world. It will show you all the features of the NetBeans IDE used by Java developers. It goes to great lengths in explaining different ways of achieving a desired goal and uncovering features present in NetBeans that are often overlooked or forgotten.

The NetBeans IDE 7 Cookbook will appeal to Java programmers at all levels who are ready to go beyond just tutorials.

Publication date:
May 2011


Chapter 1. NetBeans Head First

In this chapter, we will cover:

  • Creating a Java Project using Wizard

  • Creating libraries

  • Sharing libraries

  • Importing Projects from Eclipse

  • Importing existing Maven Projects



How to start a project successfully is often overlooked, but is always important. Projects often consist of many components that are built separately and then combined together to create a larger system. Because of this, we will look at how to create and share libraries so we can benefit from modularity and a more decentralized system. This will also help in configuring your environment to work with, for example, checking project folders into and out of version control systems.

Due to the decentralized nature of software engineering, we will take a look at how to import projects from different sources. This will give us a greater opportunity to work with various teams that use different sets of tools while still benefiting from NetBeans's capabilities.


Creating a Java Project Using Wizard

Let's start with a Java project. There is not much configuration to be done with this kind of project, but this will help in familiarizing with the IDE. It will also will give some insight on how to create other, more advanced projects since the flow is almost identical.

Getting ready

If you do not have NetBeans installed on your machine yet, please visit:

It is also necessary to have Java installed; please visit:

How to do it...

  1. Let's create a New Project. This can be achieved by either clicking File and then New Project, or by pressing Ctrl+Shift+N.

  2. In the New Project window, two panes will be shown. Choose Java on the Categories pane and Java Application on the Proejcts pane, then click Next.

  3. At the Name and Location step, Project Name should be filled with HelloWorld.

  4. At Create Main Class, append com to the beginning of helloworld.Main, giving com.helloworld.Main.

  5. Leave all the other options marked and click Finish.

The configuration window should look like the following screenshot:

How it works...

While the project location can be specified, since we did not change the folder, NetBeans automatically adds the project to NetBeansProjects, which is the NetBeans default project folder.

Use dedicated Folder for Storing Libraries is marked since we would like to have a specific project folder where all of the libraries will be stored, making packaging and distribution easier in the future. Libraries are packaged as JAR files. A JAR file is an example of a library that contains Java sources, which enable the developer to extend the functionality of applications.

NetBeans will automatically generate a main class called Main, add a main() method, and generate default comments through the class when the Create Main Class option is marked.

NetBeans will create the package structure, which will consist of Source Packages and Test Packages. The code templates provided by the IDE will correctly place the files in their respective folders. For example, if one JUnit test is created, the file will be placed under the Test Packages folder automatically.

One node containing libraries will also be shown in the projects view. This is where NetBeans will copy the necessary libraries. In the Java project, for example, NetBeans copies and configures the JUnit libraries into our newly-created project by default. Different projects will have different dependencies.

There's more...

You should play a bit with other project wizards. Most of the popular frameworks are supported in NetBeans IDE. It is good to get to know them.


Creating libraries

We will create a library folder upon project creation. For this recipe, we will create a Java EE project.

A library consists of a set of files that are packaged inside a compressed file. Many files can exist inside a library but usually, in Java, libraries contain only class, JARs, and configuration files. Libraries in Java can have different extensions.

Libraries in Java can have different extensions:

  • JAR: An acronym for Java Archive. This file extension is built on top of a normal ZIP file to aggregate various files.

  • WAR: Web Application Archive means that this file is responsible for obtaining Web Application related files. Example: images, Java files, JSPs, Servlets, XMLs, other JARs, and so on.

  • EAR: The Enterprise Archive usually encompasses all the other files combined. Normally it contains other WARs (and will also hold the same file types), configuration types, and other JARs. This file is better suited to compact into a single file, many different modules of an application.

Both WAR and EAR are built on top of JAR. These files will be explored in future recipes so for now we will only work with normal JAR files.

Getting ready

Please refer to the Getting ready section of the previous recipe for necessary tools and configurations.

Once that is complete, it is possible to continue to the next section.

How to do it...

  1. Create a New Project by either clicking File and then New Project, or pressing Ctrl+Shift+N (shortcut).

  2. In the New Project window, choose Java Web on the Categories side and Web Application on the Projects, then click Next.

  3. At Name and Location, click on Use Dedicated Folder for Storing Libraries.

  4. Now, either type the folder path or select one by clicking on Browse.

  5. After choosing the folder enter Project Name as CreatingLibraries and, proceed by clicking Next and then Finish.

How it works...

Since we are creating libraries, a new project is going to be needed for this purpose. Projects can be packaged as libraries and reused in other projects to extend their functionality. Let's not worry about changing any of the default values that are set by the IDE for Project Name and web servers, since it is not a part of the scope of this recipe.

When selecting Use Dedicated Folder for Storing Libraries, it should be noted that this is where the libraries required for compilation will be stored.

NetBeans will perform the heavy lifting of copying all the necessary libraries to the specified folder. After following these steps, a new project will be created with the shared libraries folder at the chosen location.

There's more...

The usage of a Java Web project is just to exemplify; but projects in normal Java, Java ME, or Java EE would also follow these same steps with the same results.

If you are feeling curious, a good suggestion is to check the folder and file structure of the newly created lib folder.


Sharing libraries

After creating a library directory, the next step is to actually share the project's library.

When projects become mature enough, or just for testing purposes, the developer can package the entire project into compressed Java Archive Files (JAR). These compressed files might be enterprise (EAR) or web (WAR) format, or simply JARs.

For this recipe, we will be using the previously created project and creating the WAR file from it.

Getting ready

If no Project is currently available at the Projects tab, follow the Creating libraries recipe to ensure that this recipe will work.

How to do it...

  1. Click on the Projects tab, or press Ctrl+1.

  2. Right-click on the project's name and click on build.


  3. Click on Files, or press Ctrl+2, to navigate to the File structure of the project.

  4. Expand the project node by double-clicking on the project's name.

  5. Right-click on build.xml.

  6. Select Run Target.

  7. On Run Target's sub menu, click on dist.

  8. The Output panel will show the path in which the WAR file is placed.

How it works...

NetBeans uses Apache Ant as the automated building system of choice.

This script is incorporated into NetBeans menus while on the Projects panel, as seen by the build menu. By clicking on build, NetBeans will call the Ant script and initiate the process of bundling all the necessary files of this project into the WAR file.

The second step-by-step is given as an example on how to access the dist target through the Files panel.

There's more...

Unsure which way to execute? And why is it good to have a separate build.xml generated by NetBeans?

File Structure or Project Structure?

The second method, though longer, is very complete in the sense that other targets can be attained, in case the dist is not necessarily what we want.

What is the advantage of a separate ANT build file?

The advantage with NetBeans build.xml is that it can be used outside the IDE as well; very useful if you are deploying the application in a machine that does not have access to a Graphical Interface. It also lets the project be packaged without the necessity of using NetBeans.

For example, sometimes the developer needs to connect remotely to some server that does not provide a graphical window manager to solve issues/bugs/introduce features. In this case, it is easy to make necessary changes, package the solution, and deploy to the application server.


Importing Projects from Eclipse

If you wish to work alongside Eclipse or MyEclipse, NetBeans lets you use the Project Import functionality.

This functionality will import one or more projects created by the Eclipse IDE simply by specifying the workspace in which they are housed.

Getting ready

A valid Eclipse project, with sources and dependencies, must be used in order to continue with this recipe.

How to do it...

  1. Click on File and then on Import Project and Eclipse Project....

  2. In the Import Eclipse Project window, select Import Eclipse Project.

  3. Click on the Browse... button. An Open dialog will pop-up for workspace selection.

  4. After selecting a valid workspace project, click the Next > button.

  5. In the Projects to Import section, select the projects you want to import.

  6. Select Store NetBeans project data inside Eclipse project folders.

  7. Click the Finish button.

How it works...

By selecting the workspace directory, NetBeans will then analyze and convert the metadata created by Eclipse.

The project structure, along with the dependencies, will be available for NetBeans usage. It is important to notice that NetBeans will not change the way the Eclipse project behaves.

On Store NetBeans project data inside Eclipse project folders, NetBeans will create its own structure inside the Eclipse folder structure. Select this option if you want to distribute the NetBeans project directory in a Version Control System. This ensures that libraries and configuration files used by Eclipse and NetBeans are the same.

It is also possible to use NetBeans without placing its configuration files inside Eclipse's workspace. If this is the desired outcome, then select Create imported NetBeans projects in a separate location and then click on Browse... to select the folder where NetBeans configurations will exist.

The imported Eclipse projects will then be placed on the Projects tab on the right-hand side.

In the There's more… section, there is a list of some errors that might arise when importing an Eclipse project.

There's more...

Now let's talk about some other options, as well as some pieces of general information that are relevant to this task.

Synchronizing Eclipse

With multiple developers working on a project, it is common that changes to a project happen from time to time. When this happens, NetBeans can resynchronize the projects by the following:

  • File

  • Import Eclipse Project

  • Resynchronize Eclipse Projects

After following these steps, the classpaths of all the projects imported to Eclipse will be in sync. Changes to your local project will not be synchronized back, however. This way, NetBeans ensures that the local configurations will not damage the parent project.

More error dialogs

While importing Eclipse projects, some importing errors might come up.

Many of these errors are not specific to our recipe but the following notes might come in handy while developing future projects.

Some of these errors are:

  • Resolve Missing Server Problem: Just right-click on the project node and browse to the folder where the server is installed.

  • Resolve Reference Problem: Occurs when libraries are missing from the project class path. Solving this is very similar to the missing server problem. Right-click on the project node and select Resolve Reference Problem, and then select the folder where the library is.

  • Eclipse platform for Project Name cannot be used. It is a JRE and the NetBeans project requires a JDK. NetBeans will use the default platform: This is due to the Eclipse project being configured with a JRE instead of JDK. To solve this, click on the toolbar in Tools, and then select Java Platforms. The Java Platform Manager will be shown, click on Add Platform... and from the options, select the correct Java Platform the application is being developed on.


Importing existing Maven Projects

With NetBeans, Maven support is already integrated to the IDE. And with the latest release, 7.0, Maven 3.0 is integrated.

There is no requirement to download or configure plugins. It is as simple as that.

Getting ready

Note that even if Maven is not in the System Path, NetBeans already has an embedded version for ease of use. This is very useful if there is no specific Maven version to be used.

However, it is a good practice to install and configure Maven properly.

For more information, visit:

An existing Maven project should exist in order to continue with this recipe.

How to do it...

To open a Maven project:

  1. Click on File and then Open Project or press Ctrl+Shift+O.

  2. The Open Project dialog will be shown.

Select the folder where the Maven project is placed by clicking on Look In (A Maven project will have the following icon):

Once the File name changes to the correct folder, click Open Project.

How it works...

NetBeans recognizes Maven projects based on the Project Object Model (POM) files and certain other characteristics intrinsic to the format.

After clicking Open Project, the Projects tab will show the Maven structure.

Maven, like Ant, helps developers package and maintain projects. Developers can perform certain tasks by invoking Maven goals, such as packaging and executing test cases.

About the Author

  • Rhawi Dantas

    Rhawi Dantas is a software engineer from Recife, Brazil, with several years of Java development expertise, focused mainly on server-side development. He has a Bachelor's degree in Information Systems and is currently doing his Masters in Software Systems from Tampere University of Technology. He is also certified as SCJP, SCWCD, and SCSNI.

    Browse publications by this author
NetBeans IDE 7 Cookbook
Unlock this book and the full library for FREE
Start free trial