NetBeans-SCSNI

3.2-Describe how to manage the classpath of a Java SE project, including maintaining a separate classpath for compiling and debugging.

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 need to use a library in several projects, create a special project within which to store it.

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.

Whenever you build a standard projects 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 Preparing a JAR 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.

You can also add dependent projects and other required resources to a project by simply right-clicking the Libraries node in the Projects window and choosing the appropriate item (Project, Library, or JAR/Folder) from the contextual menu.

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. In order to change a free-form project's actual compilation or runtime classpath you must edit your build.xml file directly.

Note that 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 Declaring the Classpath in Free-Form Projects.

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

-- Wagner R. Santos