Creating, Importing, and Configuring Java Projects
This guide provides an overview of the NetBeans IDE's main features for setting up and configuring Java projects. In addition, it provides information on how to do custom configuration of IDE-generated build scripts.
To complete this tutorial, you need the following software and resources.
Basic Project Concepts
This section provides an overview of some background information on the IDE's project system.
In the IDE, you always work inside of a project. In addition to source files,
an IDE project contains metadata about what belongs on the classpath,
how to build and run the project, and so on.
The IDE stores project information in a project folder which includes an Ant
build script and properties file that control the build and run settings, and
Note: Though the IDE puts source directories within the project folder by default, your source directories do not necessarily need to be located in the project folder.
Apache Ant is a Java-based build tool used to standardize and automate build and run environments for development. The IDE's project system is based directly on Ant. All of the project commands, like Clean and Build Project and Debug, call targets in the project's Ant script. You can therefore build and run your project outside the IDE exactly as it is built and run inside the IDE.
It is not necessary to know Ant to work with the IDE. You can set all the basic compilation and runtime options in the project's Project Properties dialog box and the IDE automatically updates your project's Ant script. If you are familiar with Ant, you can customize a standard project's Ant script or write your own Ant script for a project.
Creating a Project
To create a new project:
When the New Project wizard appears, select the right template for your project and complete the remaining wizard steps. The project template icon can be displayed in gray, which means that this project type has not been activated. Proceed with creating the project and this functionality will be activated in the IDE.
The IDE contains the following standard project templates for Java and Java Web applications:
In addition, the IDE also contains templates for EJB modules, enterprise applications, Java ME applications, and more.
The Java and Web project categories also have free-form project templates. The free-form templates enable you to use an existing Ant script for a project but require manual configuration. See Free-Form Projects below.
When you finish creating a project, it opens in the IDE with its logical structure displayed in the Projects window and its file structure displayed in the Files window:
Note: If you need to access files and directories that are outside of your project directories, you can use the Favorites window. You open the Favorites window by choosing Window > Favorites (Ctrl-3 on Windows/Cmd-3 on OS X). You add a folder or file to the Favorites window by right-clicking in the Favorites window and choosing Add to Favorites.
Importing a Project
This section shows you how to handle the initial importing of projects into the IDE.
Importing an Eclipse Workspace
For Eclipse projects, you can use the Import Eclipse Project wizard to help you create NetBeans projects from projects in an Eclipse workspace and import the project classpaths and other settings. When you use the Import Eclipse Project wizard, you do not need to use create and configure the NetBeans project manually. Open the wizard by choosing File > Import Project > Eclipse Project. See Importing Eclipse Projects into NetBeans IDE for information on how the wizard works and how you can work simultaneously in Eclipse and NetBeans projects.
Setting Up a Java Project Based on Existing Sources
For other Java projects developed outside of NetBeans, you use an "Existing Sources" template in the New Project wizard to make a NetBeans project. In the wizard, you identify the location of the sources and specify a location for the NetBeans project metadata. You then use the Project Properties dialog box to configure the project.
To set up a NetBeans project for an existing Java application:
Setting Up a Web Project Based on Existing Sources
For web projects developed outside of NetBeans, you use an "Existing Sources" template in the New Project wizard to make a NetBeans project. In the wizard, you identify the location of the sources and specify a location for the NetBeans project metadata. You then use the Project Properties dialog box to configure the project.
Note: If the project was originally developed in Eclipse, you might be able to save time by using the Import Eclipse Project wizard instead of the New Project wizard. The Import Eclipse Project wizard enables you to import multiple projects simultaneously, including their configurations. See Importing Eclipse Projects Into NetBeans IDE.
To set up a NetBeans project for an existing web application:
There are also project templates available for free-form projects. In free-form projects, the IDE uses targets in an existing Ant script to build, run, clean, test, and debug your application. If the Ant script does not contain targets for some of these functions, the functions are unavailable for the project. To implement these functions you write targets either in your Ant script or in a secondary Ant script.
In general, it is better to use standard "With Existing Sources" project templates for importing projects. For Eclipse projects, it is best to use the Import Project feature, which creates and configures a standard project for you. Standard projects are easier to maintain in the long term. However, the free-form project templates can be useful if you have an existing Ant-based project with a complex or idiosyncratic configuration that can not be replicated within a standard project. For example, if you are importing a project with multiple source roots, each of which has a different classpath, and you can not split the source roots into different projects, it might be necessary to use a free-form project template.
This guide focuses on standard projects. For more information on setting up free-form projects, see Advanced Free-Form Project Configuration.
Configuring the Classpath and Other Project Settings
This section covers the most common tasks in setting up a project, such as setting the project's target JDK, setting the classpath, creating dependencies between projects, and sharing libraries between projects and users.
Setting the Main Project
When you develop a large application consisting of numerous source folders, it is common to split up your code into separate projects. Typically, one of these projects serves as the entry point for your application. To tell the IDE which of your projects is the main entry point for your application, you set one project to be the main project. The IDE provides commands that act on the main project. For example, running the Clean and Build Main Project command builds both the main project and all of its required projects.
To set a project as the main project:
Only one project can be the main project at a time.
Setting the Target JDK in a Project
In the IDE, you can register multiple Java platforms and attach Javadoc and source code to each platform. Switching the target JDK for a standard project does the following:
By default, the IDE uses the version of the Java SE platform (JDK) on which the IDE runs as the default Java platform for IDE projects. You can view your IDE's JDK version by choosing Help > About. The JDK version is listed in the Java field.
To run the IDE with a different JDK version:
To register a new Java platform:
To switch the target JDK of a standard project:
Managing a Project's Classpath
Adding a group of class files to a project's classpath tells the IDE which classes the project should have access to during compilation and execution. The IDE also uses classpath settings to enable code completion, automatic highlighting of compilation errors, and refactoring. You can edit the classpath declarations for an existing project in the Project Properties dialog box.
In standard Java projects, the IDE maintains separate classpaths for compiling and running your project, as well as compiling and running JUnit tests (for Java SE applications). The IDE automatically adds everything on your project's compilation classpath to the project's runtime classpath.
You can modify your project's classpath in either of the following ways:
If you have attached Javadoc and source files to a JAR file in the Library Manager, the IDE automatically adds the Javadoc and source files to the project when you register the JAR file on a project's classpath. You can step into classes and look up Javadoc pages for the classes without configuring anything else.
Managing Dependencies Between Projects
If your application consists of multiple projects, you have to set up the classpath dependencies between the projects. Typically you set up one main project containing the project main class (in Java SE projects), and several required projects. A required project is a project that has been added to another project's classpath. When you clean and build a project, the IDE also cleans and builds its required projects. The required project's Javadoc and sources are also made available to the receiving project.
You can add any required projects to your project by right-clicking the Libraries node in the Projects window, or you can add them by specifying them in the Libraries tab in the Project Properties dialog box. When you want to add a required project, select the project folder whose JAR files you want to add to the classpath (the file chooser displays the icon for IDE project folders ( ). When adding projects in the Project Properties dialog box, make sure that the Build Projects on Classpath checkbox is selected.
If you want to add a free-form project to the classpath of a standard project, you have to add the free-form project's JAR file to the standard project's classpath. To do this, you must first declare all of the free-form project's output files in the Output panel of the free-form project's Project Properties dialog box.
Sharing Project Libraries
As the NetBeans IDE project system is Ant-based, NetBeans projects are already generally portable between different users, whether or not they use the IDE. However, by default, the project build script's way of referring to libraries is dependent on factors specific to each user, particularly in the case of libraries defined in the Library Manager dialog box.
For example, normally you can put a project in a zip file and give it to another user to unpack and run. But if the project depends on a custom library that you have specified in the Libraries Manager dialog box, others might initially have problems building the project because of unresolved references to the custom library.
Another potential inconvenience is that libraries are stored in a variety of locations, based on where they originate. Libraries that come with the IDE are stored in various different folders within the IDE's installation. Examples of these include the libraries for the Swing Layout Extensions, beans binding, and database drivers.
To solve these problems, you can specify a dedicated folder for project libraries. Furthermore, you have control over whether to use an relative path or an absolute path when referring to these libraries.
This flexibility makes it easier for you to handle the following situations:
You can set a dedicated libraries folder for standard Java SE, Web, and Enterprise projects. You can set the libraries folder when you create the project (in the New Project wizard) or afterwards in the Libraries tab of the Project Properties dialog box.
To make a general Java project's libraries sharable upon project creation:
To make a web or Java EE project's libraries sharable upon project creation:
To make an existing project's libraries sharable:
Note: You can also use the Libraries node of the Project Properties dialog box to change the location of the libraries folder. If you have already specified a libraries folder, clicking Browse opens a file chooser instead of the New Libraries Folder wizard.
To view the Java SE API documentation in the NetBeans IDE, use the Source > Show Documentation command or choose Window > Other > Javadoc from the main menu to view API documentation in a separate window.
However, for some third-party libraries, API documentation is not available. In these cases, the Javadoc resources must be manually associated with the IDE.
To make the Javadoc API documentation available for the Show Javadoc command:
You can configure your application to run through Java Web Start in the IDE. For this, use the Java Web Start category in the Project Properties window. For detailed information on how to enable Java Web Start, refer to the tutorial Enabling Java Web Start in the NetBeans IDE.
To configure Java Web Start for the application:
This section explains how to build standard projects in the IDE, how to do basic customizations to the build process, and how to handle compilation errors.
Building Projects, Packages, and Files
Compilation in the IDE is simple. Assuming that your project's compilation classpath is set correctly, you need only select the project, package, or file that you want to compile and choose the appropriate Build or Compile command from the Run menu. The IDE then compiles the files.
If the Compile on Save feature is enabled for a Java project (or the Deploy on Save feature is enabled for a Java web or enterprise project), the IDE compiles your files automatically when you save them. See Compile on Save below for more information.
To build your application for deployment:
Whenever you invoke compile commands, the IDE displays the output including any compilation errors encountered in the Output window, as described in Fixing Compilation Errors below.
Compile on Save
The IDE's Compile on Save feature enables you to save time when you are running and debugging projects in the IDE. When the Compile on Save feature is enabled for a project, files are compiled when you save them. The compiled files are stored in a cache that the IDE uses when you run, test, debug, and profile the project in the IDE. This enables you to test and debug your application without having to wait for the application to be built first.
When enabled, the Compile on Save feature has the following effects:
By default, Compile on Save is enabled for new projects that you create. For existing projects, Compile on Save is turned off.
To enable or disable Compile on Save for a Java project:
Similarly, the IDE has a Deploy on Save feature for Java web and enterprise applications. When the Deploy on Save feature is enabled for a project and a project has been deployed to a server through the IDE, changed files are redeployed to the server immediately. For Deploy on Save to work on GlassFish V2, the Glassfish instance must have the Directory Deployment Enabled option selected.
To enable or disable Deploy on Save for a Java web or enterprise project:
To enable directory deployment Glassfish V2:
Fixing Compilation Errors
The IDE displays output messages and any compilation errors in the Output Window. This multi-tabbed window is displayed automatically whenever you generate compilation errors, debug your program, generate Javadoc documentation, and so on. You can also open this window manually by choosing Window > Output (Ctrl-4 on Windows/Cmd-4 on OS X).
In addition, compilation errors are shown in the Tasks window and marked in the Source Editor.
One important function of the Output window is to notify you of errors found while compiling your program. The error message is displayed in blue underlined text and is linked to the line in the source code that caused the error, as illustrated in the image below. The Output window also provides links to errors found when running Ant build scripts. Whenever you click an error link in the Output window, the Source Editor jumps to the line containing the error automatically.
Every action that is run by an Ant script, such as compiling, running, and debugging files, sends its output to the same Output window tab. If you need to save the messages displayed in the Output window, you can copy and paste it to a separate file. You can also set Ant to print the command output for each new target to a new Output window tab by choosing Tools > Options, selecting the Miscellaneous category, clicking the Ant tab, and deselecting the checkbox for the Reuse Output Tabs from Finished Processes property.
Filtering Output Files
When you create a JAR file or a WAR file, you usually want to include
just the compiled
You can create additional filters using regular expressions to control the Output files. To specify which files to exclude, right-click your project in the Projects window and choose Properties to open the Project Properties dialog box. In the left pane, click on Packaging. In the right pane, enter regular expressions in the text box to specify the files to exclude when packaging the JAR or WAR files. In addition to the default expressions, here are some additional regular expressions you can use:
For a guide to regular expression syntax, see jakarta.apache.org.
This section shows you how to run projects in the IDE and how to configure the project's main class, runtime arguments, VM arguments, and the working directory.
Running Projects and Files
For Java projects, you typically set the project that contains the program's main class as the main project. For web projects, the main project is the project that is first deployed. To run a project, package, or file, choose one of the following:
When you run the project the IDE displays any compilation errors and output in the Output window. For more, see Fixing Compilation Errors.
Note: If Compile on Save is enabled for a project, the Run Project command operates on class files that have been created when you have saved those files. The Ant build script is not used. If you have defined custom steps in the build script, those steps are not followed. If you would like the full build process to occur when you use Run Project, Debug Project, and Profile Project, disable Compile on Save.
Customizing Runtime Options
By default, the IDE does not specify a main class, runtime arguments, or JVM arguments. The runtime classpath of each standard project contains the project's compiled classes and everything in the project's compilation classpath. You can view the project's compilation classpath by opening the Project Properties dialog box and selecting the Libraries node in the Categories pane and then clicking the Compile tab in the right pane.
To change project runtime options, open the Project Properties dialog box by right-clicking the project node in the Projects window and choosing Properties. Next, select the Libraries node in the Categories pane and click the Run tab in the right pane of the dialog box. Note that to access settings for the main class, program arguments, the working directory for program execution and VM options, you have to select the Run node. In the next section we'll take a closer look at how to configure the runtime classpath.
Setting the Runtime Classpath
To add projects, libraries, JAR files, and folders to the project's runtime classpath, use the buttons on the right side of the Run-time Libraries list in the Project Properties dialog box.
If your project uses special libraries dynamically at runtime through an indirect interface or reflection (like JDBC drivers or JAXP implementations), you have to add these libraries to the runtime classpath. You also have to adjust your runtime classpath if the runtime dependencies between your projects do not match the compilation dependencies between the projects. For example, imagine that project A compiles against project B, and project B compiles against project C, but project A does not compile against project C. This means that project A only has project B on its runtime classpath. If project A requires both project B and project C during execution, you have to add project C to project A's runtime classpath.
Setting the Main Class and Runtime Arguments
To set the project's main class, select the Run node in the Categories
pane of the Project Properties dialog box and type the fully-qualified
name in the Main Class field (for example,
If you use the Browse button to choose the project main class, the file chooser only shows classes in your project source directory. If you want to specify a class in one the libraries on the classpath, you have to type the fully-qualified name of the class in the Main Class field.
Setting JVM Arguments
You can specify JVM arguments for the project in the Project Properties dialog box. Open the Project Properties dialog box and click Run in the Categories pane and then type a space-separated list of JVM arguments in the VM Options field.
You can set system properties by typing the following in the VM Options field:
Customizing the Build Script
In standard projects the IDE generates the build script based on the options you enter in the New Project wizard and the project's Project Properties dialog box. You can set all the basic compilation and runtime options in the project's Project Properties dialog box and the IDE automatically updates your project's Ant script. If you have additional requirements for the build process that can not be handled in the Project Properties dialog box, you can modify the build script directly.
The main Ant script for a standard project is
In addition, the build script uses the
With standard projects, you can customize the build process by doing any of the following:
The following table lists some common tasks for redefining a JAR file that you may find useful:
If you are looking for resources on learning Ant, see http://ant.apache.org/resources.html. You can also install the Ant manual into the IDE help system by using the Plugins Manager. Choose Tools > Plugins and install the Ant Documentation module.
Editing and Running Ant Scripts
The IDE automatically recognizes Ant scripts and displays them as Ant script nodes ( ) rather than as normal XML files. You can right-click Ant scripts in the Projects window, Files window, or Favorites window to access a pop-up menu of commands. You can also expand the Ant script node to see an alphabetical list of subnodes representing the Ant script's targets. Each of these subnodes also has a popup menu of commands.
In the Projects, Files, and Favorites windows, an Ant script's subnodes are flagged in the following ways:
Double-click any of the Ant script's subnodes to jump to that target's location in the Source Editor. All of the normal XML search tools, selection tools, and keyboard shortcuts are available for editing Ant scripts, and the IDE provides code completion for all standard Ant tasks.
When you create a target that you want to run from the command line,
give the target a description attribute. Then, if you forget the names
of the targets or what they do, you can run the ant
The font style of a subnode's label in the Projects, Files, and Favorites windows indicates the following:
Targets that are imported from another script but are overridden in the importing script are not listed. Only the overriding target is listed.
You can run targets in an Ant script from the Ant script's node in the Projects window, Files window, or Favorites window. To do so, right-click the Ant script node and choose a target from the Run Target submenu. Targets are sorted alphabetically. Only emphasized targets are listed. Choose Other Targets to run a target that has not been emphasized with a description attribute. Internal targets are excluded from these lists because they cannot be run independently.
Instead of running a target by using the Ant script node's popup menu, you can simply right-click the target's node and choose Run Target.
Writing Custom Ant Tasks
You can use custom Ant tasks to expand on the functionality provided by
Ant's built-in tasks. Custom tasks are often used to define properties,
create nested elements, or write text directly between tags using the
To create a custom Ant task in the IDE:
When you create the custom Ant task file, the template opens in the Source Editor. The template contains sample code for many of the common operations performed by Ant tasks. After each section of code, the template also shows you how to use the task in an Ant script.