Skip Headers
NetBeans Developing Applications with NetBeans IDE
Release 7.4

E40142-08
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
PDF · Mobi · ePub

5 Creating Java Projects

This chapter describes creating and working with standard and free-form Java projects, including using projects using templates.

This chapter contains the following sections:

5.1 About Creating Java Projects

A project is a group of source files and the settings with which you build, run, and debug those source files. In the IDE, all Java development has to take place within a project. For applications that involve large code bases, it is often advantageous to split your application source code into several projects

The IDE includes several project templates designed to support different types of development including web applications, general Java applications, and so forth. The IDE's set of standard project templates automatically generate an Ant script and properties. The IDE also contains free-form project templates that you can use to base a project on an existing Ant script. In addition to Ant, the IDE also supports Maven, an open source build management tool. Maven uses a project object model (POM) that describes a set of standards that all projects using Maven follow allowing for consistency between projects. You can easily create a Maven project by choosing a Maven project template and providing a few project details.

Note:

You do not need an in-depth understanding of Ant or Maven to work with the IDE as the provided set of standard project templates automatically generate an Ant script or Maven POM files based on the options you enter in the IDE.

For more information about Ant, see http://ant.apache.org/.

This chapter focuses primarily on the use of Ant scripts to build a project. For information on using Maven to build and manage a project in JDeveloper, see "Working with Maven in NetBeans" and "Working with Maven Repositories." For more information about Maven, see http://maven.apache.org/.

5.2 Using Java Project Templates

You can facilitate creating a Java application by using one of the available templates provided by the IDE. For each type of Java application, the IDE provides two types of project templates:

5.2.1 Standard Project Templates

With standard project templates, the IDE controls all aspects of how your application is built, run, and debugged. You set a project's source folder, classpath, and other project settings when creating the project and in the Project Properties dialog box. The IDE generates an Ant build script in which all of your settings are stored.

The IDE comes with the following standard templates:

  • Java Application icon Standard Java Applications:

    • Java Application - An empty Java SE project with a main class.

    • Java Class Library - An empty Java class library with no main class.

    • Java Project with Existing Sources - A Java SE project with existing sources.

  • JavaFX Application icon JavaFX Applications:

    • JavaFX Application (without FXML) - An empty JavaFX application with no main class.

    • JavaFX Application With FXML - An empty JavaFX FXML-enabled application.

    • JavaFX Preloader - A JavaFX application with a preloader to facilitate loading the application particularly in applet or webstart mode.

    • JavaFX Swing Application - A JavaFX application enabled with Swing and a main class containing sample JavaFX-n-Swing code.

  • Web Application icon Web Applications:

    • Web Application - An empty web application.

    • Web Application with Existing Sources - A web application with existing sources.

  • EJB Modules icon EJB Modules:

    • EJB Module - An empty EJB module.

    • EJB Module with Existing Sources - An EJB module with existing sources.

  • Enterprise Application icon Enterprise Applications:

    • Enterprise Application - An empty enterprise application.

    • Enterprise Application with Existing Sources - An enterprise application with existing sources that conform to the Sun Java BluePrints Guidelines.

  • Enterprise Application Client icon Enterprise Application Clients:

    • Enterprise Application Client - An empty enterprise application client.

    • Enterprise Application Client with Existing Sources - An enterprise application client with existing sources.

  • NetBeans Module icon NetBeans Modules:

    • Module Project - An empty module with a layer.xml file and a Bundle.properties file. You use a module to implement the logic that integrates the library wrappers into the platform and provides a user interface for receiving user input.

    • Module Suite Project - A library wrapped in a module project and a Bundle.properties file. You use a library wrapper to put a library JAR file on a module's classpath and export some or all of the JAR file's packages from the module as public packages.

    • Library Wrapper Module Project - An empty module suite. You use a module suite to group and deploy a set of interdependent modules and library wrappers.

Depending on what modules you have installed, your IDE may contain additional templates. Consult the help for your additional modules for more information.

For information on creating an Web Application Project, see Chapter 11, "Developing Web Applications."

For information on creating an Enterprise Application Project, see Chapter 13, "Developing Enterprise Applications."

For information on creating an EJB Module Project, see Chapter 15, "Developing with Enterprise Beans."

5.2.1.1 Source Folders

In standard projects, you can have multiple source folders and multiple JUnit test folders for each Java SE, web, and EJB project.

Right-click the Source Packages node of a project and choose Properties to add or remove source folders. A single source folder cannot, however, be added to more than one project. If you need a source folder to be part of several projects, you should create a separate project for the source folder and add it to the classpath of each of your projects.

For information on add multiple sources to a project, see Section 5.5, "Adding Multiple Sources Roots to a Project."

5.2.1.2 Project Settings

When you create a project from a standard project template, the IDE generates an Ant script that controls all aspects of program compilation, execution, and debugging.

Right-click a project node in the Projects window and choose Properties to set basic project settings in the Project Properties dialog box. All changes are immediately registered in the project's Ant script. You can also set more complex options by editing the project's Ant script and properties file directly.

For information on editing Ant build scripts, see Section 5.2.3.3, "Editing IDE-Generated Ant Scripts."

5.2.1.3 Switching a Java SE Project to a JavaFX Deployment Model

If you have an existing standard Java SE project and you want to take advantage of the JavaFX deployment model, you can quickly switch your current project by choosing Properties from the project's context menu, selecting the Deployment category, and clicking Switch Project to JavFX Deployment Model. This feature saves you the time consuming effort of creating a new JavaFX application and manually copying over the project artifacts to the new project and resetting options, removing the main class, and so on.

Instead of a main class, JavaFX projects use an extension to the Application class. When you switch the project, the IDE creates a project_nameFX.java class and automatically adds it to the Source Packages folder of your SE project. You then have the option of using JavaFX customizations, such as specifying a JavaFX runtime version or adding custom manifest entries through the Project Properties dialog box.

Note:

Be aware that once a Java SE project has been switched to a JavaFX deployment model, it cannot be automatically reverted.

5.2.1.4 Adding a JavaFX Class to a Java SE Project

You can add a JavaFX class to a Java SE project without having to manually add a JavaFX runtime dependency. This feature is particularly advantageous for making a Java class library project usable for JavaFX development. When you select the Keep JavaFX RT artifacts on Compile Classpath if not present by default option from the Deployment category of the Project Properties dialog box, the IDE automatically updates the jfxrt.jar file dependency. If you later change the project platform, the dependency is corrected for you.

5.2.1.5 Project Folders

Each standard project has a project folder where the IDE stores the Ant script, project metadata, and output folders. In projects with existing sources, you can place the project source directories in the same location as the project folder or in a separate location. In empty projects, the source root is always in the same location as the project directory.

Table 5-1 Standard Project Folder Contents

Item Description

build.xml

The build script called by the IDE. This build script only contains an import statement that imports targets from nbproject/build-impl.xml. Use the build.xml to override targets from build-impl.xml or to create new targets.

nbproject

The directory that contains the project Ant script and other metadata.This directory contains:

  • build-impl.xml. The IDE-generated Ant script. You should never edit build-impl.xml directly. Always override its targets in build.xml.

  • project.properties. Ant properties used by the IDE to configure the Ant script. Although you can edit this file manually, you generally do not have to, as it is automatically updated by the IDE when you configure the project's properties.

  • project.xml and genfiles.properties. IDE-generated metadata files. Although you can edit project.xml manually, for standard projects it is generally not necessary. Never edit genfiles.properties.

nbproject/private

The directory that holds properties that are defined for you only. If you are sharing the project over VCS, any properties you define in private.properties are not checked in with other project metadata and are applied only to your installation of the IDE.

build

The output directory for compiled classes.

dist

The output directory of packaged build outputs (JAR files and WAR files). Standard Java projects produce one build output per project. The dist directory also contains generated Javadoc documentation.


For information on setting the classpath for standard projects, see Section 5.2.3.1, "Managing the Classpath."

5.2.2 Free-Form Templates

With free-form project templates, the IDE relies on your existing Ant script for instructions on how to compile, run, and debug your applications. The settings you configure in the New Project wizard when creating a project as well as in the Project Properties dialog box are used to tell the IDE how your Ant script manages your source code and must be consistent with the settings in your Ant script.

For example, all classpath elements are handled by your Ant script. When you declare the classpath for a free-form project, you are only telling the IDE which classes to make available for code completion and refactoring. These settings do not affect the actual classpath used when compiling or running your source code.

Free-form projects can contain as many source folders as your Ant script is configured to handle. If your Ant script does not contain targets for all IDE actions, like debugging and running your project, you can easily write Ant targets for these actions.

The standard distribution of the IDE contains the following Free-form Project icon free-form Java project templates:

  • Java Free-Form Project. A free-form project containing one or more Java source roots.

  • Web Free-Form Project. A free-form project containing a web application and optionally other Java source roots.

5.2.2.1 Source Folders

Free-form Java projects can contain multiple source folders. The classpath relationships and handling instructions for your source directories must be handled in your Ant script.

5.2.2.2 Project Settings

In the project's properties, you declare the source folders, classpath, and output files for your project. Each source root can have a unique classpath and output file. These settings do not affect the actual contents or classpath of your application. The settings only tell the IDE how to handle the code. For example, the classpath settings tell the IDE which classes to make available for code completion.

5.2.2.3 IDE Commands and Ant Targets

In free-form projects, the IDE relies on your Ant script to provide targets for all IDE actions, such as running, debugging, and generating Javadoc. If your Ant script does not contain targets for these commands, the commands are disabled.

You can write debug targets in your Ant script or in a supplementary Ant script. You can also add a shortcut to any of your Ant script's targets to the contextual menu of your project's node in the Projects window.

For a full guide to configuring free-form projects, see the following: https://netbeans.org/kb/articles/freeform-config.html.

For information on creating free-form projects in the IDE, see Section 5.2.4, "Creating Free-Form Projects."

5.2.3 Creating Standard Projects

You can facilitate creating a Java application by using one of the available templates provided by the IDE.

To create a Java project:

  1. Choose File > New Project (Ctrl+Shift+N).

  2. Select the appropriate template for your project.

  3. Follow the steps in the remainder of the application wizard.

Once you create your project, you can configure the classpath and add a JAR file, library, or an IDE project to the classpath as needed. Set your project as the main project to build it and then run it. If necessary, set the main class and any arguments for your project prior to running it. Afterwards, you can debug your project by setting breakpoints or watches and running the IDE's debugging tool.

Note:

Maven uses repositories that contain build artifacts and project dependencies. To create a Maven project, you need to configure Maven settings at both the IDE level and at the project level. For information on creating a Maven project, see Section 7.12, "Working with Maven in NetBeans."

5.2.3.1 Managing the 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.

Source roots must only exist in a single project and cannot be shared with other projects, regardless of whether they are opened or not. If you have to use a library in several projects, create a special project within which to store it.

5.2.3.1.1 Classpath and Standard Projects

For standard projects, the IDE maintains separate classpaths for compiling and running your project, as well as compiling and running JUnit tests. The IDE automatically adds everything on your project's compilation classpath to the project's runtime classpath. Also the project's compiled test files and everything on the tests' compilation classpath are added to the test runtime classpath.

Whenever you build a standard project for which a main class is specified, the IDE automatically copies any JAR files on the project's classpath to the dist/lib folder. The IDE also adds each of the JAR files to the Class-Path element in the application JAR's manifest.mf file. This simplifies running the application outside the IDE. For more information, see Section 7.9, "Preparing a JAR File for Deployment Outside the IDE."

To edit a standard project's classpath:

  1. Expand the project node, right-click the Libraries node, and choose Properties.

  2. In the Project Properties dialog box, add the necessary elements to the project's compilation classpath by clicking the appropriate button. You can add any of the following:

    • Project. The JAR file or WAR file produced by another project, as well as the associated source files and Javadoc documentation.

    • Library. A collection of JAR files or folders with compiled classes, which can optionally have associated source files and Javadoc documentation.

    • JAR/Folder. A JAR file or folder that contains compiled classes.

  3. (Optional) In web applications, click the Deploy checkbox if you do not want to package an element in the web application. By default, all classpath elements are included in the web application.

  4. (Optional) Click the Build Projects on Classpath checkbox if you do not want to rebuild all projects on the classpath whenever you build the current project. By default, all projects on the classpath and, in web applications, projects listed in the Packaging page, are rebuilt when you build the current project.

  5. (Optional) Click the Move Up and Move Down buttons to alter the classpath priority.

  6. (Optional) Click the Run, Compile Tests, or Run Tests tabs to make any changes to the these classpaths.

In addition to other libraries, you can add annotation processors to the classpath or the processor path of your project. The annotation processors are specified as either a library, a JAR file, or another NetBeans IDE project. You can add annotation processors on the following tabs: Compile and Processor. Additionally, you can specify the FQN (fully qualified name) of the annotation processor on the Compile tab.

Adding annotation processors to project's classpath on the Compile tab. When an annotation processor is packaged together with its annotations as a single JAR file, specify this JAR file on the Compile tab. In this case, there is no need to add it to the Processor tab. The resources added on the Compile tab correspond to the –classpath option of the Java compiler.

Adding annotation processors to the processor path on the Processor tab. When an annotation processor and its annotations are packaged into separate JAR files, add the JAR file with the processor on the Processor tab and the JAR file with the annotations on the Compile tab. The resources added on the Processor tab correspond to the -processor path option of the Java compiler.

5.2.3.1.2 Classpath and Free-form Projects

In free-form projects, your Ant script handles the classpath for all of your source folders. The classpath settings for free-form projects only tell the IDE what classes to make available for code completion and refactoring. To change a free-form project's actual compilation or runtime classpath you must edit your build.xml file directly. For information on setting runtime arguments, see Section 9.6, "Setting the Main Class and Runtime Arguments."

Free-form projects do not have Library nodes nor do free-form project's Project Properties dialog boxes include a Libraries panel. For more information, see Section 5.2.4.5, "Declaring the Classpath in a Free-Form Project."

Note:

The project's classpath declaration must exactly match the classpath used by your Ant script.

Adding a free-form project to the classpath of a standard project does nothing unless you also declare the free-form project's build outputs in the Output page of its Project Properties dialog box.

For information on specifying the target JDK to use for your project, see Section 5.8, "Setting the Target JDK."

5.2.3.2 Creating Dependencies Between Projects

When creating Java applications you typically set up a single main project, which contains both the project main class and any required projects. Whenever you create separate standard project for each of a project's source roots, you have to set up the classpath dependencies between the main project and the required projects. A required project is a project that has been added to another project's classpath.

For standard projects that have a main class specified, the IDE automatically copies any JAR files on the project's classpath to the dist/lib folder when you build the project. The IDE also adds each of the JAR files to the Class-Path element in the application JAR's manifest.mf file. This simplifies running the application outside the IDE. For more information, see Section 7.9, "Preparing a JAR File for Deployment Outside the IDE."

Whenever you clean and build a project, the IDE also cleans and builds its required projects. The required project's attached Javadoc and source code are also made available to the project that you are building. For information on adding Javadoc, see Section 5.7.1, "How to Add a Javadoc to a Project."

If you want to add a free-form project to the classpath of a standard project, you have to declare all of the JAR files that are produced when the free-form project is built. Right-click the free-form project's node in the Projects window and choose Properties. Then specify those JAR files in the Output page of the dialog box.

To configure project dependencies:

  1. In the Projects window, expand the node of the project to which you want to add a dependent project.

  2. Right-click the Libraries node and choose Add Project.

  3. Select the directory containing the project whose JAR files you want to add to the classpath. When you select the project directory, the project name and project JAR files are displayed in the dialog's right pane.

    The file chooser depicts IDE project directories using Project Folder icon project folder icons.

  4. Click Add Project JAR Files.

    The IDE adds the dependent project to the primary project's classpath and display a node for it within the Projects window's Library node.

In free-form projects, the classpath is defined in your Ant script. For more information, see Section 5.2.4.5, "Declaring the Classpath in a Free-Form Project."

For information on modifying the runtime classpath, see Section 9.5, "Setting the Runtime Classpath."

5.2.3.3 Editing IDE-Generated Ant Scripts

If you are working with a standard project and want to set more project options than are available in the Project Properties dialog box, you can edit the project's Ant script and Ant properties. The Ant targets and properties are defined in the following files in your project folder:

  • nbproject/build-impl.xml. The Ant script that contains all of the instructions for building, running, and debugging the project. Never edit this file. However, you can open it to examine the Ant targets that are available to be overridden.

  • build.xml. The main Ant script for the project. The IDE calls targets in this Ant script whenever you run IDE commands. build.xml contains a single import statement that imports targets from build-impl.xml. In build.xml, you can override any of the targets from build-impl.xml or write new targets.

  • nbproject/project.properties. The Ant properties file that contains important information about your project, such as the location of your source and output folders. You can override the properties in this file.

  • nbproject/private/private.properties. The Ant properties file that contains properties specific to your installation of the IDE. For example, if you are sharing the project over VCS, any properties you set in private.properties are not checked into the repository. You can copy a property from project.properties into private.properties and give the property different definitions in each file. The definitions in private.properties take precedence over those in project.properties.

Note:

In a free-form project, there is no build-impl.xml. The IDE directly calls targets in the project's Ant script.

For NetBeans Plugin Module projects, build-impl.xml imports targets from the suite.xml in your target platform's harness folder.

For information on editing and running an Ant script, see Section 7.2.2, "How to Edit an Ant Script" and Section 7.2.3, "How to Run an Ant Script," respectively.

5.2.3.4 Customizing the IDE-Generated Ant Script

You can edit the Ant scripts generated for standard projects to fine tune the way your project is built and run.

To override a target from an IDE-generated build script:

  • Copy the target from build-impl.xml or suite.xml to build.xml and make any changes to the target.

    Note:

    You might need to disable the Compile on Save option in project properties, because when enabled it causes some commands to skip calling the ant targets. To disable the Compile on Save option, right-click your project in the Projects window, select Properties, and clear the Compile on Same checkbox in the Compiling category.

To add instructions to be processed before or after an Ant target is run:

  • Override the -pre or -post target for the target.

    For example, to insert an obfuscator after compilation, type the following in build.xml:

    <target name="-post-compile">
      <obfuscate>
        <fileset dir="${build.classes.dir}"/>
      </obfuscate>
    </target>
    

To add a new target to the build script:

  1. Create the new target in build.xml.

  2. Optionally, add the new target to the dependencies of any of the IDE's existing targets. Override the existing target in build.xml then add the new target to the existing target's depends property.

    For example, the following adds the new-target target to the run target's dependencies:

    <target name="run" depends="new-target,init,compile"/>
    

    Copy the dependencies exactly as they exist in build-impl.xml or suite.xml. Notice that you do not have to copy the body of the run target into build.xml.

Note:

The output folder is deleted every time you clean your project. Therefore, never set the output folder to the same location as your source folder without first configuring the clean target to not delete the output folder.

For information on editing and running an script, see Section 7.2.2, "How to Edit an Ant Script" and Section 7.2.3, "How to Run an Ant Script," respectively.

For information on setting JVM arguments and system properties, see Section 9.7, "Setting JVM Arguments."

5.2.3.5 Working with a JavaFX Project

To work with JavaFX in NetBeans IDE, you must have the following:

  • Installed JavaFX 2 SDK and Runtime

  • A JavaFX-enabled Java Platform within the IDE

5.2.3.5.1 JavaFX Application and JavaFX FXML Applications

In a JavaFX Application without FXML, graphic layout and actions are both defined in Java classes. By default, both are defined in the main Application class.

In a JavaFX FXML application, graphic layout is defined in an FXML file and actions are defined in a controller Java class. The controller class implements the interface javafx.fxml.Initializable.

5.2.3.5.2 JavaFX Preloaders

A preloader is a small application that is started before the main application to customize the startup experience. You can create JavaFX preloaders either by running the New JavaFX Preloader wizard or by selecting Create Custom Preloader in the New JavaFX Application wizard.

5.2.3.5.3 Editing JavaFX Applications

Edit JavaFX Java source code as you edit any other Java source code in the IDE. For more information, see Section 6.2, "Editing Java Code."

Edit FXML files in one of two ways, depending on whether you have SceneBuilder installed:

  • If SceneBuilder is installed, go to the Projects window and either double-click the FXML file's node or right-click the FXML file's node and select Open. SceneBuilder opens automatically, focused on that FXML file. For information about installing and using SceneBuilder, see the JavaFX SceneBuilder documentation at http://docs.oracle.com/javafx/index.html.

  • If SceneBuilder is not installed, edit the FXML file as you edit any XML document in the IDE. If SceneBuilder is installed, you can choose to use the IDE's XML editor instead by right-clicking the FXML file's node and selecting Edit. For more information, see Section 17.2, "Creating and Editing XML Documents."

5.2.3.5.4 Building and Running JavaFX Applications

Build and run a JavaFX application as you build and run a standard Java application, with the difference that you can configure a JavaFX project to run in the following ways:

  • Standalone application. Project builds and runs as a standard Java desktop application.

  • Java Web Start. Application uses JNLP. The IDE generates the JNLP page when it builds the application.

  • In Browser. The IDE embeds the application in a browser, using an HTML page that calls a JNLP page. The IDE can generate the HTML page or you can use your own web page. If you run the application from the IDE, the IDE opens a browser page that points to the HTML page on your local file system.

To set how a JavaFX application is run, open the Project Properties and go to the Run category. To open Project Properties, right-click the project's node in the Projects window and select Properties from the context menu. For more information on packaging a JavaFX application, see the chapter Packaging Basics in the JavaFX Documentation at:

http://docs.oracle.com/javafx/2/deployment/packaging.htm

5.2.3.5.5 Debugging JavaFX Applications

Debug JavaFX applications as you would debug Java applications. In addition, you can use the Java GUI visual debugger with JavaFX applications. For more information on JavaFX, see the JavaFX 2 documentation at http://docs.oracle.com/javafx/.

5.2.4 Creating Free-Form Projects

Free-form projects do not produce an Ant script or hold project metadata in another form. Instead, these projects rely on your existing Ant script to provide instructions for handling your project source.

To create a free-form project:

  1. Choose File > New Project (Ctrl+Shift+N).

  2. Select the appropriate template for your project.

  3. Follow the steps in the remainder of the application wizard.

5.2.4.1 Source Folders

Free-form Java projects can contain multiple source folders. The classpath relationships and handling instructions for your source directories must be handled in your Ant script.

5.2.4.2 Project Settings

In the project's properties, you declare the source folders, classpath, and output files for your project. Each source root can have a unique classpath and output file. These settings do not affect the actual contents or classpath of your application. The settings only tell the IDE how to handle the code. For example, the classpath settings tell the IDE which classes to make available for code completion.

For information on how to declare a classpath, see Section 5.2.4.5, "Declaring the Classpath in a Free-Form Project."

5.2.4.3 IDE Commands and Ant Targets

In free-form projects, the IDE relies on your Ant script to provide targets for all IDE actions, such as running, debugging, and generating Javadoc. If your Ant script does not contain targets for these commands, the commands are disabled.

You can write debug targets in your Ant script or in a supplementary Ant script. You can also add a shortcut to any of your Ant script's targets to the contextual menu of your project's node in the Projects window.

For a full guide to configuring free-form projects, see https://netbeans.org/kb/articles/freeform-config.html.

For information on mapping an Ant target to a command in the IDE, see Section 5.2.4.6, "Mapping an Ant Target to an IDE Command."

For information on setting the IDE to read targets from a separate Ant script, see Section 5.2.4.8, "Storing IDE Targets in a Separate Ant Script."

5.2.4.4 Adding a Source Directory to a Free-Form Project

Free-form projects can contain multiple source directories, as long as the project's Ant script contains instructions on handling those source directories.

To add a source directory to a free-form project:

  1. Build the project so that all of the project's build outputs (JAR files and WAR files) exist on your computer.

  2. In the Projects window, right-click the project node and choose Properties.

  3. Click Sources in the right panel of the Project Properties dialog box and add the source folder.

  4. Click Classpath in the right panel of the Project Properties dialog box and set the classpath for the source directory.

  5. Click Output in the right panel of the Project Properties dialog box and specify the source folder's build output.

  6. Click OK.

5.2.4.5 Declaring the Classpath in a Free-Form Project

In a free-form project, all classpath relationships between your source folders are handled by your Ant script. In order for the IDE to know which classes to include in code completion and refactoring, you have to declare the classpath in the project settings.

You first declare the classpath in the New Project wizard when creating the project. You can edit all classpath declarations for an existing project in the Project Properties dialog box.

Declaring the classpath in the Project Properties dialog box does not change the actual compilation or runtime classpath of the source folders. The project's classpath declaration must exactly match the classpath used by your Ant script.

To declare the classpath for a project:

  1. Build the project so that all of the project's build outputs (JAR files and WAR files) exist on your computer.

  2. In the Projects window, right-click the project node and choose Properties.

  3. Click Java Sources Classpath in the Categories panel of the Project Properties dialog box.

  4. Declare the classpath for the project.

    • To set a common classpath for all source folders, unselect the Separate Classpath for Each Source Package Folder checkbox. Then add or remove classpath elements with the buttons on the right of the dialog box.

    • To set a separate classpath for each source folder, leave the Separate Classpath for Each Source Package Folder checkbox selected. Select a source folder in the drop-down list and add or remove classpath elements for the source folder.

    • To add the build output of source folder A to source folder B, select the source folder B in the drop-down list and click Add JAR/Folder. Then navigate to the output folder of source folder A and select its build output. You must have a separate classpath for each source package folder to set up relational classpath dependencies between source package folders.

    If you have attached Javadoc and source files to a JAR file in the Ant 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.

For information on how to add multiple source directories to a free-form project, see Section 5.2.4.4, "Adding a Source Directory to a Free-Form Project."

For information on adding Javadoc documentation associated with a JAR file, see Section 5.7.1, "How to Add a Javadoc to a Project."

For information on adding source code to a JAR file or a compiled classes folder, see Section 9.8.3, "Attaching Source Code to a JAR File."

5.2.4.6 Mapping an Ant Target to an IDE Command

In a free-form project, IDE commands must be mapped to targets in your Ant script. These mappings are recorded in the project.xml file in your project folder.

Mappings are recorded in the project.xml file automatically in the following cases:

  • When you specify a target for a command in the Build and Run Actions page of the New Project wizard.

  • When you specify a target for a command in the Build and Run panel of the Project Properties dialog box for a project.

  • When you create a mapping for a command after having chosen the command from the Build menu or Run menu and having been prompted to create the mapping.

  • When you have had the IDE generate a target. (The IDE offers to generate a target for the Debug Project and Compile File commands the first time you choose these commands in a free-form project.)

You can also use the Project Properties dialog box to add a shortcut to any Ant target to the project node's context menu. For example, if you have several runnable classes that you have to run often, you can write targets to run these classes and then run them using the links. Right-click the project node, choose Properties, and register the shortcuts in the Build and Run page of the dialog box.

You map other commands, like those that run on individual files, by manually editing project.xml.

To map commands for a project:

  1. In the Projects window, right-click the project node and choose Properties.

  2. Click Build and Run in the right panel of the Project Properties dialog box.

  3. For each command, choose an Ant target from the combo box. The combo box contains each of the targets in your Ant script.

  4. Use the Custom Menu Items list to add shortcuts to Ant targets to the project's contextual menu.

If your Ant script uses an <import> target to import targets from another Ant script, these targets only appear if the <import> target specifies the full path to the secondary Ant script. If the <import> target uses a property to reference the secondary Ant script, the targets do not show up in the drop-down lists. In this case, you have to type the name of the projects in the drop-down list for the command. You can also map the IDE commands to the targets by editing the project.xml file as described below.

To map IDE commands in project.xml:

  1. In the Files window, expand the node for your project folder and expand the nbproject folder.

  2. Double-click project.xml to open it in the Source Editor.

  3. Enter the following in <ide-actions>:

      <action name="action_name">
        <target>target_name</target>
      </action>
    

    If you want to map an action to a target in a separate Ant script, add the following before the <target> declaration:

     <script>path_to_Ant_script</script>
    

    You can map any of the following IDE actions:

    • debug. Debug project.

    • build. Build project.

    • run. Run project.

    • test. Run tests for project.

    • run.single. Run the currently selected file.

    • debug.single. Compile the currently selected file.

    • test.single. Run the test file for the currently selected file.

    • debug.test.single. Run the test file for the currently selected file in the debugger.

    • debug.fix. Run the Apply Code Changes command on the currently selected file.

    Note that actions that run on the currently selected files in the IDE require additional configuration in project.xml.

  4. If you also want the command to appear in the project's contextual menu, enter the following in <context-menu>:

      <ide-action name="action_name"/>
    

For information on creating a debug target for a free-form project's build script, see Section 9.8.4.1, "How to Create a Debug Target for a Free-form Java Project."

For a full guide to configuring free-form projects, see https://netbeans.org/kb/articles/freeform-config.html.

5.2.4.7 Debugging Free-Form Projects

Similar to commands for compiling and running, debugging commands rely on various information, such as the location of your sources, the location of the compiled classes and other items on the classpath, and name of the project's main class.

In free-form projects, the IDE is not aware of any of these things. When you run a command in the IDE (such as Build), the IDE simply calls a target in your build script and lets the script handle the command. Therefore, for debugging to work, you also have to have a build script target for debugging. The IDE provides some custom Ant tasks to work with the debugger and also can generate a basic debug target, which attempts to fill in important details based on other targets in your script.

For more information on debugging free-form projects, see Section 9.8.4, "Debugging Free-form Projects."

5.2.4.8 Storing IDE Targets in a Separate Ant Script

When using a free-form project, you sometimes have to write Ant targets for IDE actions. For example, you have to write an Ant target to run a program in the debugger or to compile the currently selected file in the IDE.

If you are not able to write these IDE targets in your project's Ant script, you can set the IDE to read these targets from a separate Ant script.

To map an IDE command to an Ant target in a separate Ant script:

  1. Write the target in a separate Ant script.

  2. In the Files window, expand your project folder node and the nbproject node.

  3. Double-click project.xml to open it in the Source Editor.

  4. Enter the following in <ide-actions>:

      <action name="action_name">
        <script>path_to_Ant_script</script>
        <target>target_name</target>
      </action>
    

    Note that <script> must precede <target>.

  5. If the command also appears in the project's contextual menu, enter the following in <context-menu>:

     <ide-action name="action_name"/>
    

For a full guide to configuring free-form projects, see https://netbeans.org/kb/articles/freeform-config.html.

For information on mapping Ant targets to IDE commands, see Section 5.2.4.6, "Mapping an Ant Target to an IDE Command."

5.2.4.9 Editing the project.xml File

Each IDE project has a project.xml file that includes important information about the project, such as:

  • Information about what the project's type (free-form or standard, Web application or standard Java SE application)

  • Mappings between project commands and targets in an Ant script

  • Information about the project's contents, classpath, and target Java platform. This information is used to visualize the project and enable code completion and refactoring.

For standard projects, there is usually no need to edit the project.xml file. In free-form projects, you often have to edit the project.xml file to hook up Ant targets to IDE commands and make other customizations.

5.2.4.9.1 Using Properties in the project.xml File

Like all XML files, you can define properties inside the XML file itself or store them in a separate .properties file. One way of keeping your project.xml page synchronized with the information in your Ant script is to import properties into project.xml from the same .properties file that is used by your Ant script.

To create and import properties in project.xml

  1. In the Files window, double-click project.xml.

  2. Enter the following between the <name> element and the <folders> element:

    <properties>
      <property name="name">value</property>
      <property-file>my-properties-file.properties</property-file>
    </properties>
    

    Note:

    Note that you while you can add properties in any order, properties can only refer to other properties that have been defined previously in the file. The properties file path itself can also use property substitutions.

5.2.4.9.2 Validating the project.xml File

The IDE comes bundled with the XML schemas for free-form project.xml files and automatically validates a free-form project.xml file every time you edit and save it. You can view the XML schemas for the free-form project.xml file at the following locations:

For a full guide to configuring free-form projects, see https://netbeans.org/kb/articles/freeform-config.html.

5.3 Importing an Eclipse or JBuilder Project

You can import projects created in other IDEs, such as Eclipse or JBuilder, into the NetBeans IDE.

The functionality to import Eclipse projects is included into the standard NetBeans IDE distribution. For Eclipse projects, you can import a set of dependent projects from an Eclipse Workspace or import a single project ignoring dependencies.

For JBuilder projects, you must install the JBuilder Project Importer plugin from the NetBeans Update Center. For JBuilder projects, you can import a standard Java SE project with dependencies.

To import an Eclipse project into the NetBeans IDE:

  1. Make sure that you have copy of the project that you want to import on your system.

    Typically, this project would already be in an Eclipse workspace on your system.

  2. Choose File > Import Project > Eclipse Project.

For details on how to import Eclipse projects, see Importing Eclipse Projects into NetBeans IDE at https://netbeans.org/kb/docs/java/import-eclipse.html.

To install the JBuilder Project Importer plugin:

  1. Choose Tools > Plugins from the main menu.

  2. Click the Available Plugins tab and select JBuilder Project Importer.

  3. Click Install.

To import a JBuilder project:

For information on how to create a Java project, see Section 5.1, "About Creating Java Projects."

For information on adding dependent projects, see Section 5.2.3.2, "Creating Dependencies Between Projects."

5.4 Setting the Main Project

When you develop a large application consisting of numerous source directories, it is common to split up your code into separate projects. Of these projects, one is typically the entry point for your application and contains the application's main class.

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 Build Main Project command builds both the main project and all of its required projects, thereby ensuring that you all of your compiled classes are up-to-date. Only one project can be the main project at any time.

After you set a project as the main project, the keyboard shortcuts for Run (F6), Build (F11) and Clean and Build (Shift+F11) apply to the main project regardless of which project is selected in the Projects window. Keyboard shortcuts for Debug and Profile also apply to the main project.

To make a project the main project:

Note:

If you create separate projects for each source root, you must set the classpath dependencies between the main project and the required projects. For more information, see Section 5.2.3.2, "Creating Dependencies Between Projects."For information on setting and modifying a project's classpath, see Section 5.2.3.1, "Managing the Classpath."

5.5 Adding Multiple Sources Roots to a Project

You can add multiple sources roots to either standard or free-form projects that require them. However, never add the same source root to more than one project because this is not supported in the IDE.

In Standard projects, it is sometimes advantageous to add additional source roots to your project's classpath such as when the project uses sources stored in a separate resources folder. Because all source directories on a standard project's classpath form a single compilation unit, they are also packaged into the application's JAR when the project is built. In Standard projects, you add source roots to the classpath using the project wizard and properties dialog.

In free-form projects, each source root can be assigned to independent compilation units providing you maximum flexibility when structuring your applications. Free-form projects, however, require that you manage the classpath by editing your build.xml file directly. For more information, see Section 5.2.3.1, "Managing the Classpath."

Note:

For both standard and free-form projects, source roots must only exist in a single project and cannot be shared with other projects, regardless of whether they are opened or not. If you have to use a library in several projects, create a special project within which to store it.

If you want to add a library to multiple projects, you must create a special project within which to store the library's compiled source that you want to use.

For more information on using project templates, see Section 5.2, "Using Java Project Templates."

5.6 Sharing a Library with Other Users

You can configure most standard Java SE, Web, and Enterprise projects in a way that makes it easy to share libraries with other users. You can specify a location for libraries on which the project relies. You can also specify how the libraries are referenced from your project.

These options make it easy for you to handle the following situations:

5.6.1 How to Share a Library

You can make a project's libraries sharable when you create the project in the New Project wizard. You can convert an existing project to be sharable in the Libraries tab of the Project Properties dialog box.

To make a general Java project's libraries sharable upon project creation:

  1. Choose File > New Project.

  2. In the Java category of the wizard, select one of the standard templates and click Next.

  3. In the Name and Location page of the wizard, select the Use Dedicated Folder for Sharing Libraries checkbox.

  4. In the Libraries field, select the location for the libraries to be stored.

    If the libraries are already included in the IDE, those libraries are copied to the folder that you have designated.

To make a web or enterprise project's libraries sharable upon project creation:

  1. Choose File > New Project.

  2. Select one of the standard templates in the Web or Enterprise category and click Next.

  3. In the Name and Location page of the wizard, select the Use Dedicated Folder for Sharing Libraries checkbox.

  4. In the Libraries field, select the location for the libraries to be stored.

    If the libraries are already included in the IDE, those libraries are copied to the folder that you have designated.

  5. (Optional) On the Server and Settings page, select the Copy Server JAR Files to Libraries Folder radio button.

To make an existing project's libraries sharable:

  1. Right-click the project's node and choose Properties.

  2. In the Project Properties dialog box, select the Libraries node.

  3. In the Libraries panel, click Browse to open the New Libraries Folder wizard.

  4. In the Library Folder page of the wizard, enter a location for the libraries and click Next.

    You can enter the location as a relative reference or an absolute reference.

  5. In the Actions panel of the wizard, verify the selected action for each listed library. In most cases, the IDE detects the most appropriate action for that library.

    The following actions are available:

    • Copy Library JAR Files to New Libraries Folder. Use this option if the library is not in the folder that you have selected and you want to have the library JAR files placed there.

    • Use Relative Path to Library JAR Files. Use this option if the library is not in the libraries folder and you have to access the library in its existing location using a relative path. An entry with a relative reference is added for the library in the libraries folder's nblibraries.properties file.

    • Use Absolute Path to Library JAR Files. Use this option if the library is not in the libraries folder and you need to access the library in its existing location using an absolute path. An entry with an absolute reference is added for the library in the libraries folder's nblibraries.properties file.

    • Use Existing Library in Libraries Folder. Use this option if there is already a copy of the library in the libraries folder and you want to use that copy of the library.

  6. Click Finish to exit the Make Project Sharable wizard.

  7. Click OK to exit the Project Properties dialog box.

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, click Browse to open a file chooser instead of the New Libraries Folder wizard.

Note:

Free-form project libraries cannot be sharable in the ways described in this topic.

For information on creating standard Java projects, see Section 5.2.3, "Creating Standard Projects."

For information on creating free-form projects, see Section 5.2.4, "Creating Free-Form Projects."

5.7 Adding a Javadoc to a Project

You can make Javadoc documentation for a JAR file's class available in the IDE by associating that documentation with the JAR file.

If you want to have access to compiled JDK documentation through the IDE, see Section 2.15.2, "How to Add the JDK Javadoc to the IDE."

5.7.1 How to Add a Javadoc to a Project

When you add a required project to a project's classpath, the required project's Javadoc and sources are automatically added to the project as well.

To add Javadoc for a JAR file:

  1. Choose Tools > Libraries from the main menu.

  2. In the left pane of the Ant Library Manager, select the project library within which the JAR file you want to add Javadoc documentation to is located.

    Only libraries already registered with the IDE are listed in the Ant Library Manager's Class Libraries list.

  3. If the JAR file for which you want to add Javadoc documentation has not already been added to a registered library, create a new empty library using the New Library button. Next, in the Classpath tab click Add JAR/Folder and specify the location of the JAR file containing the compiled class files.

    Note:

    You can also associate the Javadoc with a JAR file using the project's Project Properties window. However, doing so creates the association only for that project. Open the Project Properties dialog box by right-clicking the project node and choosing Properties. Select the Libraries node in the Categories pane. Then select the JAR with which you want to associate the Javadoc and click Edit. You can then specify the sources to be associated.

    A class library can contain multiple JAR files as well as their Javadoc documentation and source code.

  4. In the Javadoc tab, click Add ZIP/Folder and specify the location of the Javadoc files.

  5. Click OK to exit the Ant Library Manager.

    The IDE adds the selected JAR files and Javadoc documentation ZIP files to the specified library and automatically registers the documentation in every project that has that JAR file on its classpath.

    When you create a Java class library for a single JAR file, you can simply add the JAR file to the project's classpath to make the associated Javadoc and source code available. If your Java library contains multiple JAR files, however, you must add the library itself to the classpath. Adding the library to the classpath also makes it easier to share the project with other developers.

For information on setting the classpath for standard projects, see Section 5.2.3.1, "Managing the Classpath."

For information on adding source code to a JAR file or a compiled classes folder, see Section 9.8.3, "Attaching Source Code to a JAR File."

For information on how to view Javadoc in the IDE, see Section 2.15.3, "How to View Javadoc Documentation."

5.8 Setting the Target JDK

By default, the IDE uses the version of the Java SE platform (JDK) with which the IDE runs as the default Java platform for compilation, execution, and debugging. You can view your IDE's JDK version by choosing Help > About and clicking the Detail tab. The JDK version is listed in the Java field.

You can run the IDE with a different JDK version by starting the IDE with the --jdkhome  jdk-home-dir switch on the command line or in your IDE-HOME/etc/netbeans.conf file. For more information, see Section 2.7, "Setting Startup Parameters."

You cannot enable JavaFX for the default registered Java platform. However, you can use the same JDK sources as the default platform in a new registered Java platform and enable JavaFX in the new platform.

5.8.1 How to Register a New Java Platform

In the IDE, you can register multiple Java platforms and attach Javadoc and source code to each platform. For example, if you want to work with the new features introduced in JDK 7, you would either run the IDE on JDK 7 or register JDK 7 as a platform and attach the source code and Javadoc to the platform. You can also enable JavaFX for multiple platforms.

To register a new Java platform:

  1. Choose Tools > Java Platforms from the main window.

  2. Click Add Platform and select the directory that contains the Java platform. Java platform directories are marked with aPlatform icon icon in the file chooser. Click Next.

    If support for Java ME is enabled in the IDE you must select to add either a Java Standard Edition platform or one of the Java ME platforms. For information on adding a new Java ME platform, see Section 21.14, "Using Java ME Emulator Platforms."

  3. Specify the display name for the platform and the location of the sources for the Java platform and the Javadoc. Click Finish.

5.8.2 How to Set the Target JDK

In standard projects, you can switch the target JDK in the Project Properties dialog box. In free-form projects, you have to set the target JDK in the Ant script itself, then specify the source/binary format in the Project Properties dialog box.

To set the default Java platform for a standard project:

  1. Right-click the project's root node in the Projects window and choose Properties.

  2. In the Project Properties dialog box, select the Libraries node in the left pane.

  3. Choose the desired Java platform in the Java Platform combo box.

    Switching the target JDK for a standard project does the following:

    • Offers the new target JDK's classes for code completion.

    • If available, displays the target JDK's source code and Javadoc documentation.

    • Uses the target JDK's executables (javac and java) to compile and execute your application.

    • Compiles your source code against the target JDK's libraries.

  4. Click Manage Platforms to register additional Java platforms with the IDE.

  5. Click Add Platform and navigate to the desired platform

To set the target Java platform for a free-form project:

  1. In your Ant script, set the target JDK as desired in the javac, java, and javadoc tasks.

  2. Right-click the project's root node in the Projects window and choose Properties.

  3. In the Sources panel, set the level of JDK you want your application to be run on in the Source/Binary Format combo box.

    When you access Javadoc or source code for JDK classes, the IDE searches the Java platforms registered in the Java Platform Manager for a platform with a matching version number. If no matching platform is found, the IDE's default platform is used instead.

For information on setting the classpath for standard projects, see Section 5.2.3.1, "Managing the Classpath."

For information on how to declare a classpath, see Section 5.2.4.5, "Declaring the Classpath in a Free-Form Project."

5.9 Moving, Copying, and Renaming a Project

When you create a project, the IDE bases much of its project metadata on the name and location of the project. These values are used in the project build scripts, the project properties, and the names and locations of the project folders.

5.9.1 How to Move, Copy, or Rename a Project

To safely move, copy, and rename a project, you must use the commands provided by the IDE.

To move, copy, or rename a project:

  • Right-click the project's node in the Projects window and choose one of the following commands:

    • Rename Project

    • Move Project

    • Copy Project

Note:

When moving or copying a project to a new location, the new folder must already exist on the computer. You can create the folder with the Browse button.

Moving and renaming a project can break compilation dependencies between projects. To fix the project dependencies, remove the renamed or moved project from the classpath of any other projects, then add the project again

5.10 Deleting a Project

Deleting a project is a simple procedure and you have the option of deleting all project files or only the build scripts and metadata.

To delete a project:

  1. Right-click the project's node in the Projects window and choose Delete Project.

  2. Specify whether to also delete the source folders under the project folder. If you do not select this option, the IDE only deletes the generated build scripts and project metadata.

The IDE does not offer to delete any source folders that are outside of the project folder.