creating, importing, and configuring java projects

20
Creating, Importing, and Configuring Java Projects This guide provides an overview of the NetBeans IDE's main features f or setting up and configuring Java projects. In addition, it provides information on how to do custom configuration of IDE-generated build scripts. Contents   Basic Project Concepts o Projects o Ant  Creating a Project  Importing a Project o Importing an Eclipse Workspace  o Setting Up a Java Project Based on Existing Sources o Setting Up a Web Project Based on Existing Sources o Free-Form Projects  Configuring the Classpath and Other Project Settings  o Setting the Main Project o Setting the Target JDK in a Project  o Managing a Project's Classpath  o Managing Dependencies Between Projects o Sharing Project Libraries o Making the JDK's Javadoc Available in the IDE o Enabling Java Web Start   Building Application s o Building Projects, Packages, and Files  o Compile on Save o Fixing Compilation Errors o Filtering Output Files  Running Applications o Running Projects and Files o Customizing Runtime Options  o Setting the Runtime Classpath 

Upload: hiscribd

Post on 06-Apr-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 1/20

Creating, Importing, and Configuring Java

Projects

This guide provides an overview of the NetBeans IDE's main features for setting up and configuring Java

projects. In addition, it provides information on how to do custom configuration of IDE-generated buildscripts.

Contents 

  Basic Project Concepts 

o  Projects 

o  Ant 

  Creating a Project 

  Importing a Project 

o  Importing an Eclipse Workspace 

o  Setting Up a Java Project Based on Existing Sources 

o  Setting Up a Web Project Based on Existing Sources 

o  Free-Form Projects 

  Configuring the Classpath and Other Project Settings 

o  Setting the Main Project 

o  Setting the Target JDK in a Project 

o  Managing a Project's Classpath 

o  Managing Dependencies Between Projects 

o  Sharing Project Libraries 

o  Making the JDK's Javadoc Available in the IDE 

o  Enabling Java Web Start 

  Building Applications 

o  Building Projects, Packages, and Files 

o  Compile on Save 

o  Fixing Compilation Errors 

o  Filtering Output Files 

  Running Applications 

o  Running Projects and Files 

o  Customizing Runtime Options 

o  Setting the Runtime Classpath 

Page 2: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 2/20

o  Setting the Main Class and Runtime Arguments 

o  Setting JVM Arguments 

  Customizing the Build Script 

o  Editing and Running Ant Scripts 

o  Writing Custom Ant Tasks 

  See Also 

To complete this tutorial, you need the following software and resources. 

Software or Resource Version Required

NetBeans IDE version 6.9 or 7.0

Java Development Kit (JDK)   version 6

Basic Project Concepts

This section provides an overview of some background information on the IDE's project system.

Projects

In the IDE, you always work inside of a project. In addition to source files, an IDE project contains metadata

about what belongs on the classpath, how to build and run the project, and so on. The IDE stores project

information in a project folder which includes an Ant build script and properties file that control the build and

run settings, and a project.xml file that maps Ant targets to IDE commands.

Note: Though the IDE puts source directories within the project folder by default, your source directories do

not necessarily need to be located in the project folder.

Ant

Apache Ant is a Java-based build tool used to standardize and automate build and run environments for

development. The IDE's project system is based directly on Ant. All of the project commands, like Clean and

Build Project and Debug, call targets in the project's Ant script. You can therefore build and run your project

outside the IDE exactly as it is built and run inside the IDE.

It is not necessary to know Ant to work with the IDE. You can set all the basic compilation and runtime

options in the project's Project Properties dialog box and the IDE automatically updates your project's Ant

script. If you are familiar with Ant, you can customize a standard project's Ant script or write your own Ant

script for a project.

Creating a Project

To create a new project: 

  Choose File > New Project (Ctrl-Shift-N).

When the New Project wizard appears, select the right template for your project and complete the remaining

wizard steps. In the releases later than NetBeans IDE 6.7, the project template icon can be displayed in

gray, which means that this project type has not been activated. Proceed with creating the project and this

functionality will be activated in the IDE.

The IDE contains the following standard project templates for Java desktop and web applications:

Page 3: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 3/20

  Java Application. Creates a skeleton Java SE project with a main class. See Developing and Deploying

General Java Applications for a brief tutorial that uses this template.

  Java Desktop Application. Creates an application based on the Swing Application Framework. Skeletons

are offered for a basic desktop application and a database application that makes use of the Beans Binding

and Java Persistence API libraries. See Building a Java Desktop Database Application for an example that

uses this template.

  Java Class Library. Creates a skeleton Java class library without a main class. See Developing and

Deploying General Java Applications for a brief tutorial that uses this template.

  Java Project with Existing Sources. Creates a Java SE project based on your own Java sources.

See Setting Up a Java Project Based on Existing Sources below.

  Web Application. Creates a skeleton web application, including options to add various web frameworks.

See Introduction to Developing Web Applications for an example that uses this template.

  Web Application with Existing Sources. Creates a web project based on your own web and Java sources.

See Setting Up a Web Project Based on Existing Sources below.

In addition, the IDE also contains templates for EJB modules, enterprise applications, Java ME applications,

and more.

The Java and Web project categories also have free-form project templates. The free-form templates enable

you to use an existing Ant script for a project but require manual configuration. See Free-form

Projects below.

When you finish creating a project, it opens in the IDE with its logical structure displayed in the Projects

window and its file structure displayed in the Files window:

  The Projects window is the main entry point to your project sources. It shows a logical view of important

project contents such as Java packages and Web pages. You can right-click any project node to access a

popup menu of commands for building, running, and debugging the project, as well as opening the ProjectProperties dialog box. The Projects window can be opened by choosing Window > Projects (Ctrl-1).

  The Files window shows a directory-based view of your projects, including files and folders that are not

displayed in the Projects window. From the Files window, you can open and edit your project configuration

files, such as the project's build script and properties file. You can also view build output like compiled

classes, JAR files, WAR files, and generated Javadoc documentation. The Files window can be opened by

choosing Window > Files (Ctrl-2).

Page 4: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 4/20

 

Note: If you need to access files and directories that are outside of your project directories, you can use the

Favorites window. You open the Favorites window by choosing Window > Favorites (Ctrl-3). You add

a folder or file to the Favorites window by right-clicking in the Favorites window and choosing Add to

Favorites.

Importing a Project

This section shows you how to handle the initial importing of projects into the IDE.

Importing an Eclipse Workspace

For Eclipse projects, you can use the Import Eclipse Project wizard to help you create NetBeans projects

from projects in an Eclipse workspace and import the project classpaths and other settings. When you use

the Import Eclipse Project wizard, you do not need to use create and configure the NetBeans project

manually. Open the wizard by choosing File > Import Project > Eclipse Project. See Importing

Eclipse Projects into NetBeans IDE. for information on how the wizard works and how you can work

simultaneously in Eclipse and NetBeans projects.

Setting Up a Java Project Based on Existing Sources

For other Java projects developed outside of NetBeans, you use an "Existing Sources" template in the New

Project wizard to make a NetBeans project. In the wizard, you identify the location of the sources and

specify a location for the NetBeans project metadata. You then use the Project Properties dialog box

to configure the project. 

To set up a NetBeans project for an existing Java application: 

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

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

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

o  Type a project name.

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

o  (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.

Page 5: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 5/20

o  (Optional) Select the Use Dedicated Folder for Storing Libraries checkbox and specify the location

for the libraries folder. See Sharing Project Libraries for more information on this option.

o  (Optional) Select the Set as Main Project checkbox. When you select this option, keyboard shortcuts

for commands such as Clean and Build Main Project (Shift-F11) 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.

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.

Setting Up a Web Project Based on Existing Sources

For web projects developed outside of NetBeans, you use an "Existing Sources" template in the New Project

wizard to make a NetBeans project. In the wizard, you identify the location of the sources and specify a

location for the NetBeans project metadata. You then use the Project Properties dialog box to configure the

project. 

Note: If the project was originally developed in Eclipse, you might be able to save time by using the Import

Eclipse Project wizard instead of the New Project wizard. The Import Eclipse Project wizard enables you to

import multiple projects simultaneously, including their configurations. See Importing Eclipse Projects Into

NetBeans IDE. 

To set up a NetBeans project for an existing web application: 

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

2.  Choose Java Web > Web Application with Existing Sources. Click Next.

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

o  In the Location field, enter the folder that contains the web application's source root folders and

web page folders.

o  Type a project name.

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

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

for the libraries folder. SeeSharing Project Libraries in NetBeans IDE for more information on this

option.

Page 6: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 6/20

5.  (Optional) Select the Set as Main Project checkbox. When you select this option, keyboard shortcuts

for commands such as Clean and Build Main Project (Shift-F11) apply to this project.

6.  Click Next to advance to the Server and Settings page of the wizard.

7.  (Optional) Add the project to an existing enterprise application.

8.  Select a server to which to deploy. If the server that you want does not appear, click Add to register

the server in the IDE.

9.  (Optional) Select the Dedicated Library Folder for Server JAR files checkbox. This option is only

available if you have designated a dedicated folder for libraries on the previous page of the wizard.

10.  Set the source level to the Java version on which you want the application to run.

11.  (Optional) Adjust the context path. By default, the context path is based on the project name.

12.  Click Next to advance to the Existing Sources and Libraries page of the wizard.

13.  Verify all of the fields on the page, such as the values for the Web Pages Folder and Source Package

Folders.

14.  Click Finish.

Free-Form Projects

There are also project templates available for free-form projects. In free-form projects, the IDE uses targets

in an existing Ant script to build, run, clean, test, and debug your application. If the Ant script does not

contain targets for some of these functions, the functions are unavailable for the project. To implement

these functions you write targets either in your Ant script or in a secondary Ant script.

In general, it is better to use standard "With Existing Sources" project templates for importing projects. For

Eclipse projects, it is best to use the Import Project feature, which creates and configures a standard project

for you. Standard projects are easier to maintain in the long term. However, the free-form project templates

can be useful if you have an existing Ant-based project with a complex or idiosyncratic configuration that

can not be replicated within a standard project. For example, if you are importing a project with multiple

source roots, each of which has a different classpath, and you can not split the source roots into different

projects, it might be necessary to use a free-form project template.

This guide focuses on standard projects. For more information on setting up free-form projects,

see Advanced Free-Form Project Configuration. 

Configuring the Classpath and Other Project Settings

This section covers the most common tasks in setting up a project, such as setting the project's target JDK,

setting the classpath, creating dependencies between projects, and sharing libraries between projects and

users.

Setting the Main Project

When you develop a large application consisting of numerous source folders, it is common to split up your

code into separate projects. Typically, one of these projects serves as the entry point for your application.

To tell the IDE which of your projects is the main entry point for your application, you set one project to be

the main project. The IDE provides commands that act on the main project. For example, running the Clean

and Build Main Project command builds both the main project and all of its required projects.

Page 7: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 7/20

To set a project as the main project: 

  Right-click the project node in the Projects window, and choose Set as Main Project.

Only one project can be the main project at a time.

Setting the Target JDK in a Project

In the IDE, you can register multiple Java platforms and attach Javadoc and source code to each platform.

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

  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.

By default, the IDE uses the version of the Java SE platform (JDK) on which the IDE runs as the default Java

platform for IDE projects. You can view your IDE's JDK version by choosing Help > About. The JDK version

is listed in the Java field.

To run the IDE with a different JDK version: 

  Start the IDE with the following switch from the command line.

--jdkhome jdk-home-dir

Alternatively, set the JDK in the /etc/netbeans.conf file that is located in the IDE's installation folder.

To register a new Java platform: 

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

2.  Specify the folder that contains the Java platform as well as the sources and Javadoc needed for

debugging.

Page 8: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 8/20

 

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.

Managing a Project's ClasspathAdding a group of class files to a project's classpath tells the IDE which classes the project should have

access to during compilation and execution. The IDE also uses classpath settings to enable code completion,

automatic highlighting of compilation errors, and refactoring. You can edit the classpath declarations for an

existing project in the Project Properties dialog box.

In standard Java projects, the IDE maintains separate classpaths for compiling and running your project, as

well as compiling and running JUnit tests (for Java SE applications). The IDE automatically adds everything

on your project's compilation classpath to the project's runtime classpath.

You can modify your project's classpath in either of the following ways:  

  Right-click the project's node, choose Properties, select the Libraries category, and modify the listed

classpath entries.

  Right-click the Libraries node in the Projects window and choose Add Project, Add Library, or Add

JAR/Folder.

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.

Page 9: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 9/20

Managing Dependencies Between ProjectsIf your application consists of multiple projects, you have to set up the classpath dependencies between the

projects. Typically you set up one main project containing the project main class (in Java SE projects), and

several required projects. A required project is a project that has been added to another project's classpath.

When you clean and build a project, the IDE also cleans and builds its required projects. The required

project's Javadoc and sources are also made available to the receiving project.

You can add any required projects to your project by right-clicking the Libraries node in the Projects window,

or you can add them by specifying them in the Libraries tab in the Project Properties dialog box. When you

want to add a required project, select the project folder whose JAR files you want to add to the classpath

(the file chooser displays the icon for IDE project folders ( ). When adding projects in the Project

Properties dialog box, make sure that the Build Projects on Classpath checkbox is selected.If you want to add a free-form project to the classpath of a standard project, you have to add the free-form

project's JAR file to the standard project's classpath. To do this, you must first declare all of the free-form

project's output files in the Output panel of the free-form project's Project Properties dialog box.

Sharing Project Libraries

As the NetBeans IDE project system is Ant-based, NetBeans projects are already generally portable between

different users, whether or not they use the IDE. However, by default, the project build script's way of 

Page 10: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 10/20

referring to libraries is dependent on factors specific to each user, particularly in the case of libraries defined

in the Library Manager dialog box.

For example, normally you can put a project in a zip file and give it to another user to unpack and run. But if 

the project depends on a custom library that you have specified in the Libraries Manager dialog box, others

might initially have problems building the project because of unresolved references to the custom library.

Another potential inconvenience is that libraries are stored in a variety of locations, based on where they

originate. Libraries that come with the IDE are stored in various different folders within the IDE's

installation. Examples of these include the libraries for the Swing Layout Extensions, beans binding, and

database drivers.

To solve these problems, you can specify a dedicated folder for project libraries. Furthermore, you have

control over whether to use an relative path or an absolute path when referring to these libraries.

This flexibility makes it easier for you to handle the following situations:

  You create a project and need to make it available and buildable to other users, whether they use the IDE or

not. They should be able to access the project through a version control checkout or by unpacking a zip file

that you have provided them. Then they should be able build the application without extra configuration.

  You must start working on an existing project and adhere to a strict set of conventions concerning whereproject libraries are stored (and whether your build script accesses them with a relative reference or

absolute reference).

You can set a dedicated libraries folder for standard Java SE, Web, and Enterprise projects. You can set the

libraries folder when you create the project (in the New Project wizard) or afterwards in the Libraries tab of 

the Project Properties dialog box.

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

1.  Choose File > New Project.

2.  In the Java category of the wizard, select one of the standard templates. Then 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 Folder field, select the location for the libraries to be stored.

If the libraries are already included in the IDE, those libraries will be copied to the folder that you

have designated.

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

1.  Choose File > New Project.

2.  In the Web category of the wizard, select one of the standard templates. Then 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 Folder field, select the location for the libraries to be stored.

If the libraries are already included in the IDE, those libraries will be 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.

Page 11: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 11/20

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:

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

o  Use Relative 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 a relative path. An entry with a

relative reference is added for the library in the libraries folder'snblibraries.properties file.

o  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'snblibraries.properties file.

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

Note: You can also use the Libraries node of the Project Properties dialog box to change the location of the

libraries folder. If you have already specified a libraries folder, clicking Browse opens a file chooser instead

of the New Libraries Folder wizard.

Making the Javadoc Available in the IDE

To view the JavaSE API documentation in the NetBeans IDE, use the Source > Show

Documentation command or choose Window >Other > Javadoc from the main menu to view API

documentation in a separate window.

However, for some third-party libraries, API documentation is not available. In these cases, the Javadoc

resources must be manually associated with the IDE.

To make the Javadoc API documentation available for the Show Javadoc command: 

1.  Download the Javadoc API documentation source.

2.  Choose Tools > Libraries.

3.  In the Libraries list, select the library that your project is using.

4.  Click the Javadoc tab.

Page 12: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 12/20

5.  Click the Add ZIP/Folder button and navigate to the zip file or the folder that contains the Javadoc

API documentation on your system. Select the zip file or the folder and click the Add ZIP/Folder

button.

6.  Click OK.

Enabling Java Web Start for the ApplicationYou can configure your application to run through Java Web Start in the IDE. For this, use the Java Web

Start category in the Project Properties window. For detailed information on how to enable Java Web Start,

refer to the tutorial Enabling Java Web Start in the NetBeans IDE. 

To configure Java Web Start for the application: 

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

2.  In the Project Properties dialog box, select the Application > Web Start node.

3.  Select the Enable Web Start checkbox and configure Java Web Start settings as explained

in Enabling Java Web Start in the NetBeans IDE. 

Building ApplicationsThis section explains how to build standard projects in the IDE, how to do basic customizations to the build

process, and how to handle compilation errors.

Building Projects, Packages, and Files

Compilation in the IDE is simple. Assuming that your project's compilation classpath is set correctly, you

need only select the project, package, or file that you want to compile and choose the appropriate Build or

Compile command from the Run menu. The IDE then compiles the files.

If the Compile on Save feature is enabled for a Java project (or the Deploy on Save feature is enabled for a

Java web or enterprise project), the IDE compiles your files automatically when you save them. See Compile

on Save below for more information.

To build your application for deployment: 

  Choose Run > Clean and Build Project.

Any previously compiled files and distributable outputs such as JAR files are deleted. Then the files are

recompiled and new outputs are produced. A build folder is created to hold the compiled classes. A dist is

generated to contain distributable outputs such as JAR files and Javadoc documentation. You can see the

build outputs by opening the Files window.

Whenever you invoke compile commands, the IDE displays the output including any compilation errors

encountered in the Output window, as described in Fixing Compilation Errors below.

Compile on Save

The IDE's Compile on Save feature enables you to save time when you are running and debugging projects

in the IDE. When the Compile on Save feature is enabled for a project, files are compiled when you save

them. The compiled files are stored in a cache that the IDE uses when you run, test, debug, and profile the

project in the IDE. This enables you to test and debug your application without having to wait for the

application to be built first.

When enabled, the Compile on Save feature has the following effects:

  The IDE compiles your files as you save them so that your project is always ready to run or debug in the

IDE.

Page 13: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 13/20

  The Build command is disabled. If you want to rebuild the JAR file without having to use the Clean and Build

command, you need to disable the Save on Compile feature.

  The project's Ant script is not used when you use Run, Debug, Profile, or Test commands. If you have

customized your build script for any of these commands, those customizations will be disregarded.

  The JAR file that is built from the project is not kept up to date when you save changes to your files. If you

want to build or rebuild the JAR file, you need to use the Clean and Build command. The Clean and Build

command always uses your project's Ant script, whether or not Compile on Save is enabled.

By default, Compile on Save is enabled for new projects that you create. For existing projects, Compile on

Save is turned off.

To enable or disable Compile on Save for a Java project: 

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

2.  Select the Compiling node and set the Compile on Save property.

Similarly, the IDE has a Deploy on Save feature for Java web and enterprise applications. When the Deploy

on Save feature is enabled for a project and a project has been deployed to a server through the IDE,changed files are redeployed to the server immediately. For Deploy on Save to work on GlassFish V2, the

Glassfish instance must have the Directory Deployment Enabled option selected.

To enable or disable Deploy on Save for a Java web or enterprise project: 

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

2.  Select the Run node and set the Deploy on Save property.

To enable directory deployment Glassfish V2: 

1.  Choose Tools > Servers.

2.  Select the server.

3.  Select the Options tab.

4.  Select the Directory Deployment Enabled option.

Fixing Compilation Errors

The IDE displays output messages and any compilation errors in the Output Window. This multi-tabbed

window is displayed automatically whenever you generate compilation errors, debug your program, generate

Javadoc documentation, and so on. You can also open this window manually by choosing Window > Output

(Ctrl-4).

In addition, compilation errors are shown in the Tasks window and marked in the Source Editor.

One important function of the Output window is to notify you of errors found while compiling your program.

The error message is displayed in blue underlined text and is linked to the line in the source code that

caused the error, as illustrated in the image below. The Output window also provides links to errors found

when running Ant build scripts. Whenever you click an error link in the Output window, the Source Editor

 jumps to the line containing the error automatically. You can also use the F12 and Shift-F12 keyboard

shortcuts to move to the next and previous error in the file.

Page 14: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 14/20

 

Every action that is run by an Ant script, such as compiling, running, and debugging files, sends its output to

the same Output window tab. If you need to save the messages displayed in the Output window, you can

copy and paste it to a separate file. You can also set Ant to print the command output for each new target to

a new Output window tab by choosing Tools > Options, selecting the Miscellaneous category, clicking the

Ant tab, and deselecting the checkbox for the Reuse Output Tabs from Finished Processes property.

Filtering Output Files

When you create a JAR file or a WAR file, you usually want to include just the compiled.class files and

any other resource files located in your source directory, such as resource bundles or XML documents. The

default filter does this for you by excluding all.java, .nbattrs, and .form files from your output file.

You can create additional filters using regular expressions to control the Output files. To specify which files

to exclude, right-click your project in the Projects window and choose Properties to open the Project

Properties dialog box. In the left pane, click on Packaging. In the right pane, enter regular expressions in the

text box to specify the files to exclude when packaging the JAR or WAR files. In addition to the default

expressions, here are some additional regular expressions you can use:Regular Expression Description

\.html$  Exclude all HTML files

\.java$  Exclude all Java files

(\.html$)|(\.java$)  Exclude all HTML and Java files

(Key)|(\.gif$) Exclude all GIF files and any files with Key in their

name

For a guide to regular expression syntax, see  jakarta.apache.org. 

Running Applications

Page 15: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 15/20

This section shows you how to run projects in the IDE and how to configure the project's main class, runtime

arguments, VM arguments, and the working directory.

Running Projects and Files

For Java projects, you typically set the project that contains the program's main class as the main project.

For web projects, the main project is the project that is first deployed. To run a project, package, or file,

choose one of the following:  In the main menu, choose Run > Run Main Project (F6) to run the main project. Alternatively, you can

use the Run Main Project button in the toolbar.

  In the Projects window, right-click the project and choose Run to run a project. Note that for Java projects,

the project must have a main class.

  In the Projects window, right-click the file and choose Run File (Shift+F6) to run a file. Alternatively,

choose Run > Run File> Run filename (Shift+F6) in the main menu to run a runnable class.

When you run the project the IDE displays any compilation errors and output in the Output window. For

more, see Fixing Compilation Errors. 

Note: If Compile on Save is enabled for a project, the Run Project command operates on class files thathave been created when you have saved those files. The Ant build script is not used. If you have defined

custom steps in the build script, those steps are not followed. If you would like the full build process to occur

when you use Run Project, Debug Project, and Profile Project, disable Compile on Save. 

Customizing Runtime Options

By default, the IDE does not specify a main class, runtime arguments, or JVM arguments. The runtime

classpath of each standard project contains the project's compiled classes and everything in the project's

compilation classpath. You can view the project's compilation classpath by opening the Project Properties

dialog box and selecting the Libraries node in the Categories pane and then clicking the Compile tab in the

right pane.

To change project runtime options, open the Project Properties dialog box by right-clicking the project node

in the Projects window and choosing Properties. Next, select the Libraries node in the Categories pane and

click the Run tab in the right pane of the dialog box. Note that to access settings for the main class, program

arguments, the working directory for program execution and VM options, you have to select the Run node.

In the next section we'll take a closer look at how to configure the runtime classpath.

Page 16: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 16/20

Setting the Runtime Classpath

To add projects, libraries, JAR files, and folders to the project's runtime classpath, use the buttons on the

right side of the Run-time Libraries list in the Project Properties dialog box.

If your project uses special libraries dynamically at runtime through an indirect interface or reflection (like

JDBC drivers or JAXP implementations), you have to add these libraries to the runtime classpath. You also

have to adjust your runtime classpath if the runtime dependencies between your projects do not match the

compilation dependencies between the projects. For example, imagine that project A compiles against

project B, and project B compiles against project C, but project A does not compile against project C. This

means that project A only has project B on its runtime classpath. If project A requires both project B and

project C during execution, you have to add project C to project A's runtime classpath.

Setting the Main Class and Runtime Arguments

To set the project's main class, select the Run node in the Categories pane of the Project Properties dialog

box and type the fully-qualified name in the Main Class field (for

example, org.myCompany.myLib.MyLibClass). The main class must exist in the project or in one of the

JAR files or libraries in the project's runtime classpath. Afterwards, type any necessary runtime arguments

in the Arguments field.

If you use the Browse button to choose the project main class, the file chooser only shows classes in your

project source directory. If you want to specify a class in one the libraries on the classpath, you have to type

the fully-qualified name of the class in the Main Class field.

Page 17: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 17/20

Setting JVM Arguments

You can specify JVM arguments for the project in the Project Properties dialog box. Open the Project

Properties dialog box and click Run in the Categories pane and then type a space-separated list of JVM

arguments in the VM Options field.

You can set system properties by typing the following in the VM Options field:

-Dname=value

Customizing the Build Script

In standard projects the IDE generates the build script based on the options you enter in the New Project

wizard and the project's Project Properties dialog box. You can set all the basic compilation and runtime

options in the project's Project Properties dialog box and the IDE automatically updates your project's Ant

script. If you have additional requirements for the build process that can not be handled in the Project

Properties dialog box, you can modify the build script directly.

The main Ant script for a standard project is build.xml. The IDE calls targets in build.xml whenever you

run IDE commands. This file contains an import statement that imports nbproject/build-impl.xml,

which contains build targets that are generated by the IDE. Inbuild.xml, you can override any of thetargets from nbproject/build-impl.xml or write new targets. Do not editnbproject/build-

impl.xml directly, because that file is regenerated based on changes that you make in the Project

Properties dialog box.

In addition, the build script uses the nbproject/project.properties which you can edit manually.

With standard projects, you can customize the build process by doing any of the following:

  Entering basic options, like classpath settings and JAR filters, in the New Project wizard when you create a

project, or afterwards in the Project Properties dialog box.

  Editing properties in nbproject/project.properties. This file stores Ant properties with important

information about your project, such as the location of your source and output folders. You can override the

properties in this file. Be careful when editing this file. For example, the output folder is deleted every timeyou clean your project. You should 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.

  Customizing existing or creating new Ant targets by doing any of the following:

o  Add instructions to be processed before or after an Ant target is run. Each of the main targets

in nbproject/build-impl.xml also has a -pre and -post target that you can override in build.xml.

For example, to get RMI working with regular projects, type the following in build.xml:

o  <target name="-post-compile">

o  <rmic base="${build.classes.dir}" includes="**/Remote*.class"/>

</target>

o  Change the instructions in an Ant target. Copy the target from nbproject/build-

impl.xml to build.xml and make any changes to the target.

o  Create new targets in build.xml. You can also add the new target to the dependencies of any of the IDE's

existing targets. Override the existing target in build.xml and 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:

Page 18: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 18/20

o  <target name="new-target">

o  <!-- target body... -->

o  </new-target>

o <target name="run" depends="new-target,myprojname-

impl.run"/>You do not need to copy the body of the run target into build.xml.

The following table lists some common tasks for redefining a JAR file that you may find useful:

To perform

this taskFollow these steps

Specifywhich files

are added toa JAR file.

Right-click the project node in the Projects window and chooseProperties. Click the Packaging subnode (under Build) and

configure the filter and compression settings using the Excludefrom JAR File field. For more, seeFiltering Output Files 

Change aJAR file's

name andlocation.

In the Files window, double-click the

project's nbproject/project.properties file to open it in

the Source Editor. Enter the full path to the JAR file in

the dist.jar property.

Specify themanifest file

for a JAR

file.

In project.properties, type the name of the manifest file

in the manifest.file property. The file name must be

specified relative to the project's build.xml file. Note that if 

you are using the Java Application template, the IDE creates a

manifest file for you.

Disable the

generationof a JAR file

for a project.

In the Files window, open your project folder and

open build.xml. Override the jar target to have no contents

and no dependencies. For example, add the following

to build.xml:

<target name="jar" />

If you are looking for resources on learning Ant, see http://ant.apache.org/resources.html. You can also

install the Ant manual into the IDE help system by using the Plugins Manager. Choose Tools > Plugins and

install the Ant Documentation module.

Editing and Running Ant Scripts

The IDE automatically recognizes Ant scripts and displays them as Ant script nodes ( ) rather than as

normal XML files. You can right-click Ant scripts in the Projects window, Files window, or Favorites window to

access a pop-up menu of commands. You can also expand the Ant script node to see an alphabetical list of 

subnodes representing the Ant script's targets. Each of these subnodes also has a popup menu of 

commands.

Page 19: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 19/20

In the Projects, Files, and Favorites windows, an Ant script's subnodes are flagged in the following ways:

Icon Meaning

Emphasized Ant target. These targets include a descriptionattribute, which is displayed as a tooltip. You define the target's

description attribute in the Source Editor.

Normal Ant target. A target without a description attribute.

Double-click any of the Ant script's subnodes to jump to that target's location in the Source Editor. All of the

normal XML search tools, selection tools, and keyboard shortcuts are available for editing Ant scripts, and

the IDE provides code completion for all standard Ant tasks.

When you create a target that you want to run from the command line, give the target a description

attribute. Then, if you forget the names of the targets or what they do, you can run the ant -projecthelp

<script> command from the command line. With this command, Ant lists only those targets that have a

description attribute, together with their descriptions. Especially when there are many targets in your Ant

build script, emphasizing some and de-emphasizing others can be a useful way to distinguish between those

that you use a lot and those that you use less often.

The font style of a subnode's label in the Projects, Files, and Favorites windows indicates the following:

  Normal. A target that is defined within the current Ant script.

  Italics. A target that is imported from another Ant script.

  Greyed out. An internal target that cannot be run directly. Internal targets have names beginning with '-'.

  Bold. The default target for the script, if there is one. The default target is declared as an attribute of the

project, together with other project attributes, such as its name. You define the project's default attribute in

the Source Editor.

Targets that are imported from another script but are overridden in the importing script are not listed. Only

the overriding target is listed.

You can run targets in an Ant script from the Ant script's node in the Projects window, Files window, or

Favorites window. To do so, right-click the Ant script node and choose a target from the Run Target

submenu. Targets are sorted alphabetically. Only emphasized targets are listed. Choose Other Targets to

run a target that has not been emphasized with a description attribute. Internal targets are excluded from

these lists because they cannot be run independently.

Instead of running a target by using the Ant script node's popup menu, you can simply right-click the

target's node and choose Run Target.

Page 20: Creating, Importing, And Configuring Java Projects

8/3/2019 Creating, Importing, And Configuring Java Projects

http://slidepdf.com/reader/full/creating-importing-and-configuring-java-projects 20/20

 

Writing Custom Ant Tasks

You can use custom Ant tasks to expand on the functionality provided by Ant's built-in tasks. Custom tasks

are often used to define properties, create nested elements, or write text directly between tags using

the addText method.

To create a custom Ant task in the IDE: 

1.  Right-click the package where you would like to place the task and choose New > Other.

2.  Select the Other category and the Custom Ant Task file type.

3.  Complete the wizard.

When you create the custom Ant task file, the template opens in the Source Editor. The template contains

sample code for many of the common operations performed by Ant tasks. After each section of code, the

template also shows you how to use the task in an Ant script.

Send Us Your Feedback