Skip Headers
NetBeans Developing Applications with NetBeans IDE
Release 8.0

E50452-06
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 the IDE" and "Working with Maven Repositories." For more information about Maven, see http://maven.apache.org/.

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:

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.

5.2.2.3.1 Creating a Target to Compile a Single File

If you want to be able to select files in the IDE and compile them individually, you need an Ant target for the Compile File command. The IDE offers to generate a target the first time you choose the command. The generated target looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
<project basedir=".." name="MyProjectName">
    <!-- TODO: edit the following target according to your needs -->
    <!-- (more info: https://netbeans.org/kb/archive/index.html) -->
    <target name="compile-selected-files-in-src">
        <fail unless="files">Must set property 'files'</fail>
        <!-- TODO decide on and define some value for ${build.classes.dir} -->
        <mkdir dir="${build.classes.dir}"/>
        <javac destdir="${build.classes.dir}" includes="${files}" source="1.5" srcdir="src"/>
    </target>
</project>

In the generated target, you need to specify the directory where to put the compiled class or classes. You can do so by specifying a value for the build.classes.dir property in the generated target. For example, you might add the following line to the line above the <target name="compile-selected-files-in-src"> entry:

<property name="build.classes.dir"  value="build"/>

Alternatively, you can replace the value of the provided build.classes.dir or rewrite the target entirely.

The value of the includes parameter is the value of the generated files property. The IDE uses this property to store the name of the currently selected file (or files).

Note:

You can configure multiple compile.single actions to overload the F9 shortcut and menu command with different functionality depending on what file is selected. For example, you could set up a separate compile-selected-files target for JUnit test classes, then map compile.single to that target for all sources in JUnit test directories. Or you could change the pattern to \.xml$ and map F9 to a Validate XML target for all XML files.

5.2.2.3.2 Writing a Target for the Apply Code Changes Command

The Apply Code Changes command allows you to make changes to your code during a debugging session and continue debugging with the changed code without restarting your program. The IDE contains a nbjpdareload task that you can use to write a target for the Apply Code Changes command.

A typical target for the fix command looks something like this:

<target name="debug-fix">
    <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true" >
      <classpath refid="javac.classpath"/>
      <include name="${fix.file}.java"/>
    </javac>
    <nbjpdareload>
      <fileset dir="${classes.dir}">
        <include name="${fix.file}.class"/>
      </fileset>
    </nbjpdareload>
  </target>
  • The target compiles the currently selected file using the ${fix.file} property. (In the next section you will set up the IDE to store the name of the currently selected file in this property.)

  • The nbjpdareload task reloads the corrected file in the application.

To hook this target up to the Apply Code Changes command (the same as the Fix command in previous versions of the IDE), define the following action in <ide-actions> in project.xml:

<action name="debug.fix">
    <target>debug-fix</target>
    <context>
      <property>fix.file</property>
      <folder>${src.dir}</folder>
      <pattern>\.java$</pattern>
      <format>relative-path-noext</format>
      <arity>
        <one-file-only/>
      </arity>
    </context>
  </action>
  • <property> now stores the context in the fix.file property.

  • Since you can only run the Fix command on one file at a time, you set <arity> to <one-file-only>.

  • You have to pass the full path to the .java file to the javac task and the full path to the .class file to the nbjpdareload task. You therefore set the <format> to rel-path-noext, then append .class or .java in the debug-fix target as necessary.

Note:

The IDE does not define the ${src.dir} property for you. You have to define the property or import the .properties file that the Ant is using in project.xml. See Section 5.2.4.9.1, "Using Properties in the project.xml File" for more information.

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 the IDE."

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

      Note:

      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.

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

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.1.3 Adding Annotation Processors to the Classpath

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.

To add an annotation processor to the classpath:

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

  2. Select the Libraries category in the Project Properties window.

  3. Select one of the following tabs in the Libraries panel and add the project, library or JAR that contains the annotation processor to the project's classpath.

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

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

  4. Select the Compiling node under the Build category and select Enable Annotation Processing and Enable Annotation Processing in Editor.

  5. Click Add and type the fully-qualified name of the annotation processor in the Add Annotation Processor dialog box.

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 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 make project sources available to Ant, you need to specify the classpath for the project sources. If you have any custom tasks, you also need to add these tasks to Ant's classpath.

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.

    You do this because by default the IDE ignores your environment's CLASSPATH variable whenever it runs Ant.

    Note:

    The classpath variable you set in the Project Properties dialog box does not affect the actual classpath of the project, which is specified in the Ant script. Declaring the classpath in the Project Properties dialog box does not change the actual compilation or runtime classpath of the source folders. However, the project classpath variable must match the classpath used by your Ant script in order to provide the correct information for code completion, error highlighting, and refactoring commands. You have to set an explicit classpath in your build scripts because the IDE ignores your environment's CLASSPATH variable whenever it runs Ant. If you change the classpath of one, you must change the class path of the other.

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

  6. Click OK.

Specifying the Classpath for Custom Tasks

In free-form projects, you can call up and run custom Ant tasks in your build script. For your Ant script to use customs tasks, you must include the tasks in the Ant script's classpath. For example, you may add a task to your build script to format your code with Jalopy. In order to do this, however, you have to add the Jalopy JAR file to Ant's classpath.

You can add custom tasks to Ant's classpath within the IDE by doing either of the following:

  • Providing an explicit classpath to the tasks in your build script. This is the recommended method for specifying the location of JAR files that contain custom tasks used by your Ant script, as it ensures that your build scripts will be fully portable. You can write your tasks and include instructions to compile them and produce a JAR file in the build file. To use these tasks, include the long form of taskdef, which includes a classpath. Here is a simple example of such a task:

    <project name="test" default="all" basedir=".">
        <target name="init">
            <javac srcdir="tasksource" destdir="build/taskclasses"/>
            <jar jarfile="mytasks.jar">
                <fileset dir="build/taskclasses"/>
            </jar>
            <taskdef name="customtask" classname="com.mycom.MyCustomTask">
                <classpath>
                     <pathelement location="mytasks.jar"/>
                </classpath>
            </taskdef>
        </target>
    </project>
    

    The advantage of this method is that no special preparation is needed to begin using the script. The script is entirely self-contained and portable. This method also makes it easier to develop your tasks within the IDE, as the script compiles them for you automatically.

    To make your build scripts even more robust, use a property instead of a hard-coded location to specify the classpath to your tasks. You can store the property in the build script itself or in a separate ant.properties file. You can then change the classpath setting throughout your script by simply changing the value of the specified property.

  • Configuring the Ant Classpath property in the Options window. If you cannot declare a classpath in your build script, or you are using third-party build scripts which you cannot alter, you can add tasks to Ant's classpath in the IDE in the Options window.

    Note:

    If you modify the Ant classpath in the Options window, the task is on Ant's classpath for all projects when you run Ant in the IDE.

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

There are three ways to map an IDE command to a target in an Ant script:

  • By adjusting the settings in the Build and Run page of a project's Project Properties dialog box

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

  • By having the IDE generate a target for you and then customizing this target to your needs. This works for the Debug Project and Compile File commands. The IDE offers to generate these targets the first time you run those commands in the project.

  • By manually editing the project's project.xml file.

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

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.

    Note:

    The Project Properties dialog box is the main tool for configuring free-form projects in the IDE.

  3. For each command (Build Project, Clean Project, Generate Javadoc, Run Project (free-form Java projects), Deploy Project (free-form Web projects), and Test Project), choose an Ant target from the combo box. The combo box contains each of the targets in your Ant script.

    Note:

    If your Ant script uses an import statement to import targets from another Ant script, the 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.

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

Each IDE project has a project.xml file that contains important metadata about your project's contents, the location of the project's Ant script, which targets to run for IDE commands, and other information. If you want to map commands that work on the presently selected files in the IDE, or if you want to map a command to a target in a separate Ant script, you have to edit the project.xml file by hand.

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>
    

    Note:

    The <ide-actions> element holds the mappings for IDE commands. You enter an action element with the name for any of the standard IDE actions and define the script and target to which you want to map the command.

    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:

    • build. Build project.

    • rebuild. Clean and build project.

    • compile.single. Compile selected file.

    • clean. Clean project

    • run. Run project.

    • run.single. Run the currently selected file.

    • redeploy. For Web application projects, build project, undeploy project from server, and deploy project to server.

    • test. Run tests for project.

    • 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. Debug project.

    • debug.single. Compile the currently selected file.

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

    • debug.stepinto. Execute one line of the project main class in the debugger and pause.

    • profile.test.single. Profile the JUnit test for the selected file.

    • profile. Run project in the profiler.

    • profile.single. Profile the selected file.

    • javadoc. Generate Javadoc for project.

    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 example, the following maps the Debug Project to the debug-nb target of the project's Ant script:

  <action name="debug">
    <target>debug-nb</target>
  </action>

The Ant targets for NetBeans IDE commands do not have to live in the same Ant script that you use to build and run the project. This is useful for users who cannot alter their Ant script. The following maps the Debug Project to the debug-nb target in a separate Ant script:

  <action name="debug">
    <script>path/to/my/nbtargets.xml</script>
    <target>debug-nb</target>
  </action>

Note:

<script> must precede <target>.

You can also configure a command to run multiple targets. The targets are run in the order they appear in the action. For example, the mapping for the Clean and Build Project command looks like this:

   <action name="rebuild">
      <target>clean</target>
      <target>compile</target>
   </action>

Adding Shortcuts to Project Node Contextual Menu

project.xml also has a context-menu element that controls the contents of a project node's contextual menu. If you manually add an action that is run on the project, make sure you register the action name in <context-menu> as well. If you use the Project Properties dialog box to configure a standard project command, the IDE automatically adds the command to the project's contextual menu.

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

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.1, "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 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.

Note:

All file paths in project.xml are by default relative to the project folder. If your Ant script is not located in the project folder, a classdir property that points to build/classes/ does not point to the same directory for the Ant script and for the project.xml file. (The project folder is the folder that contains your nbproject folder, not the nbproject folder itself. By default, the new free-form project wizard makes your Ant script's parent folder the project folder.)

You can solve this problem by defining properties for important paths (like project.dir) and using these properties to be more exact (for example, classdir=${project.dir}/build/classes).

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>
      <property-file>another-properties-file.properties</property-file>
    </properties>
    

    The syntax is different than the syntax used in Ant scripts.

    Note:

    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:

5.2.5 Setting Up a Java Project Based on Existing Sources

For 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:

  1. Choose File > New Project.

  2. Choose Java > Java Project with Existing Sources. Click Next.

  3. In the Name and Location page of the wizard, follow these steps:

    • Type a project name.

    • (Optional) Change the location of the project folder.

    • (Optional) Change the name of the build script used by the IDE. This might be desirable if there is already a build script called build.xml that is used to build the sources.

    • (Optional) Select the Use Dedicated Folder for Storing Libraries checkbox and specify the location for the libraries folder.

    • (Optional) Select the Set as Main Project checkbox. When you select this option, keyboard shortcuts for commands such as Clean and Build Main Project apply to this project.

  4. Click Next to advance to the Existing Sources page of the wizard.

  5. In the Source Packages Folder pane, click Add Folder. Then navigate to your sources and select the source roots, click Open.

    Note:

    When you add a folder containing source code, you must add the folder that contains the highest folder in your package tree. For example, for the com.mycompany.myapp.ui package, you add the folder that contains the com folder.

  6. (Optional) In the Test Package Folders pane, click Add Folder to select the folder containing the JUnit package folders.

  7. Click Next to advance to the Includes & Excludes page of the wizard.

  8. (Optional) In the Includes & Excludes page of the wizard, enter file name patterns for any files that should be included or excluded from the project. By default, all files in your source roots are included.

  9. Click Finish.

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.

  3. In the Workspace Location page of the wizard, select the Import Projects from Workspace radio button, and specify the workspace location. Click Next.

  4. In the Projects to Import page, select the projects that you want to import.

  5. Choose one of the following two options for storing the NetBeans project files:

    • Store NetBeans project data inside Eclipse project folders. NetBeans adds folders and files within the top-level folder of the original project.

    • Create imported NetBeans projects in a separate location. NetBeans uses the sources and libraries in the original Eclipse project folder but creates a separate folder to hold NetBeans project metadata and build outputs.

    Note:

    Typically, it is better to store NetBeans project data inside Eclipse project folders. In most cases, this means that the NetBeans project metadata will refer to sources and libraries with the same paths that are used by the Eclipse metadata. Therefore, checking out the project from a version control system on different machines should result in similar behavior both in NetBeans and Eclipse.

  6. (Applicable when web applications are being imported only.) Click Next. In the Servers page, register any servers that your projects need with NetBeans IDE.

  7. Click Finish.

After you have completed the wizard, the following dialog boxes might appear:

After you have completed the wizard and have closed any of the above informational dialog boxes, nodes for the projects will appear in the Projects window.

If there are references in your project metadata to servers or other resources that NetBeans cannot resolve, the node for the project will appear in red. You can resolve these references immediately by right-clicking the project node and choosing Resolve Reference Problems or Resolve Missing Server.

For other types of project configuration adjustments, you use the Project Properties dialog box. You open the Project Properties dialog box by right-clicking the project's node and choosing Properties.

For more information on configuring your project, see the Creating, Importing, and Configuring Java Projects tutorial at https://netbeans.org/kb/docs/java/project-setup.html#projects-configuring.

After you have imported the project, you will find the following folder and files on your system:

Version Control Considerations

If the project is checked out of a version control system, the build (or nbbuild), dist (or nbdist), and the nbproject/private folders should not be checked into that version control system.

If the project is under the CVS, Subversion, or Mercurial version control systems, the appropriate "ignore" files are created or updated for these directories when the project is imported.

Note:

Though nbproject/private should be ignored, nbproject should be checked into the version control system. nbproject contains project metadata that enables others users to open the project in NetBeans without having to import the project first.

Building and Running an imported Project

Once you have the project imported into NetBeans, you can build and run the project. All artifacts created from NetBeans build and run commands are created in the build and dist folders. NetBeans does not over-write output created from Eclipse build actions. If the Eclipse project already has build and dist folders, the NetBeans project creates folders called nbbuild and nbdist and uses those for the build outputs.

The following are some of the build and run commands are available from the Run menu:

Resynchronizing a Project

The project importer features synchronization capabilities. If the classpath in the Eclipse has changed since you initially imported it, you can use the Resynchronize Eclipse Projects feature to update the classpath in the corresponding NetBeans project.

Project resynchronization is one-way from Eclipse projects to NetBeans projects. If you make changes to the project structure in NetBeans, those changes are not propagated to the Eclipse project with the resynchronization feature. If you intend to keep both Eclipse and NetBeans projects, use the Eclipse project as the "master" project.

The IDE also resynchronizes the projects automatically if the changes to the Eclipse configuration are unambiguous and do not require your input. This automatic resynchronization occurs shortly after you open the project. If the resynchronization requires your input, you need to manually resynchronize the project.

To manually resynchronize NetBeans projects with Eclipse projects:

Note:

When you resynchronize a project, the resynchronization is performed on all projects that you have imported from the workspace.

Resolving Import Problems

When you import a project into NetBeans, there might be some things that can not be automatically resolved in NetBeans IDE. For some of these problems, a menu item, such as Resolve Missing Server Problem, appears in the contextual menu for the project. Other problems can be resolved in the Project Properties dialog box for the imported project in NetBeans IDE.

Here is a list of common import problems and their solutions.

Problem Message Solution

Resolve Missing Server Problem

Right-click the project node and choose Resolve Missing Server Problem. Then navigate to the file or folder that contains the server.

Resolve Reference Problem

Right-click the project's node and choose Resolve Reference Problem. Then navigate to the file or folder that contains the resource that is referred to from the project.

Eclipse platform for project ProjectName cannot be used. It is a JRE and the NetBeans project requires a JDK. NetBeans will use the default platform.

If you would like to change the platform that NetBeans uses for the project, choose Tools > Platforms and specify a different platform.

Eclipse project ProjectName claims to use JDK from the "{1}" directory. But this directory does not exist. NetBeans will use the default platform.

If you would like to change the platform that NetBeans uses for the project, choose Tools > Platforms and specify a different platform.

NetBeans does not support source includes/excludes per source root as Eclipse does. They were merged and it is recommended that you double check them in project's properties in Source panel.

In NetBeans, includes and excludes are declared in one place for the whole project. To check the includes and excludes in the NetBeans project, right-click the project's node and the Projects window and choose Properties. In the Project Properties dialog box, select the Sources tab and then click the Includes/Excludes button.

Import failed due to .... More details can be found in IDE's log file.

You can open the IDE's log file by choosing View > IDE Log.

Unknown project type - it cannot be imported.

You can only import the following Eclipse project types: Java Project, Java Project from Existing Ant File, Static Web, Dynamic Web, and JPA Project.


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

As the NetBeans IDE project system is Ant-based, NetBeans projects are 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.

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

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.9.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.9, "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.

To register a new Java SE Embedded platform:

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

  2. Click Add Platform and select the Remote Java Standard Edition option. Click Next.

  3. Specify the display name for the platform, the details of the remote device where an application is executed, your authentication details, and the path to the JRE on the remote device. 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 switch the target JDK of a standard project:

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

  2. Select the Libraries panel.

  3. Change the Java Platform property.

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.8.3 How to Upload Java SE Embedded JRE to a Remote Device

When no JRE is installed on a remote device, new embedded JRE must be uploaded and deployed.

To upload and deploy an embedded JRE on a remote device:

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

  2. Click Add Platform and select the Remote Java Standard Edition option. Click Next.

  3. In the Add Java Platform dialog, specify the display name for the platform, the details of the remote device where an application is executed and your authentication details. Click Create.

  4. In the Create Java SE Embedded JRE dialog, click Browse and browse for the directory with JRE create on your hard drive.

  5. Specify the path to the directory on a remote device where JRE must be installed and the required options. Click Create.

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.