NetBeans IDE 7.1.2

advertisement
NetBeans IDE 7.1.2
NetBeans IDE 7.1 introduces support for JavaFX by enabling the full compile/debug/profile
development cycle for JavaFX applications. The release also provides significant Swing GUI Builder
enhancements, CSS3 support, and tools for visual debugging of Swing and JavaFX user interfaces.
Additional highlights include Git support integrated into the IDE, new PHP debugging features,
JavaEE improvements and more.
NetBeans IDE 7.1.2 Release Information
The NetBeans IDE is an award-winning integrated development environment available for
Windows, Mac, Linux, and Solaris. The NetBeans project consists of an open-source IDE and an
application platform that enable developers to rapidly create web, enterprise, desktop, and mobile
applications using the Java platform, as well as PHP, JavaScript and Ajax, Groovy and Grails, and
C/C++.
The NetBeans project is supported by a vibrant developer community and offers extensive
documentation and training resources as well as a diverse selection of third-party plugins.
Release Overview
NetBeans IDE 7.1 introduced support for JavaFX 2.0 and NetBeans IDE 7.1.2 introduces
support for JavaFX 2.1 by enabling the full compile/debug/profile development cycle for JavaFX 2.1
applications. The release also provides significant Swing GUI Builder enhancements, CSS3 support,
and tools for visual debugging of Swing and JavaFX user interfaces. Additional highlights include Git
support integrated into the IDE, new PHP debugging features, various JavaEE and Maven
improvements, and more.
NetBeans IDE 7.1 is available in English, Brazilian Portuguese, Japanese, Russian, and
Simplified Chinese.
NetBeans IDE 7.1.2 is an update to NetBeans IDE 7.1 and NetBeans IDE 7.1.1, and contains the
following highlights:






Java SE 7u4 Support, which includes the first Oracle JDK release for Mac OS X
JavaFX 2.1 Support (Bundled with the JDK)
Ant upgrade to version 1.8.3
Support for GlassFish 3.1.2
Integration of recent patches
Minor performance improvements
New Feature Highlights
JavaFX




Support for JavaFX 2.1
Support of all three deployment models: Desktop, Applet, JNLP
Preloaders to improve application loading experience
Customized UI controls using CSS3
Java


New Visual debugger
Gap support in GridBagLayout customizer


Inspect and Refactor
Improved Javadoc highlighting and formatting
Web Languages
 Support for CSS3
 Code completion and documentation for new CSS3 elements
 Browser specific properties
Java EE
 Support for GlassFish 3.1.2
 Cluster and Instance deployment support for GlassFish
 Developing Java EE 6 applications for deployment to WebLogic Server
 50+ CDI enhancements in Java editor
 JSF component suite support
 Improvements in Java Persistence, Web Services, EJB, WebLogic and more
PHP
 Improved PHP debugging
 PHPUnit test groups capabilities
 Support for Smarty templates
 Faster uploads with keep-alive for (S)FTP client
NetBeans Platform
 New Window System APIs
 Enhanced TopComponentGroup behavior
 Visual Window Layout designer
 Pluggable multiview components
Versioning
 Integrated Git support
 History tab on file editor window
 Locking/unlocking files in Subversion working directory
 Support for Mercurial branches and tags
Addition Information
Rich Internet Application (RIA)
เป็ นเว็บแอปพลิเคชันทีม่ ีความสามารถของเดสก์ท็อปแอปพลิเคชัน โดยทัว่ ไปแล้ ว RIA
จะทางานที่เกี่ยวข้ องกับส่วนติดต่อผู้ใช้ บนเครื่ องของผู้ใช้ แต่จะเก็บข้ อมูลต่างๆ (เช่น สถานะของโปรแกรม)
ไว้ บนแอปพลิเคชันเซิร์ฟเวอร์ โดยปกติแล้ ว RIA จะ
 รันในเว็บเบราว์เซอร์ หรื อไม่ต้องติดตังซอฟต์
้
แวร์ เพิม่ เติมเป็ นพิเศษ
 ทางานบนเครื่ องของผู้ใช้ ในสภาพแวดล้ อมที่จากัด (sandbox) ด้ วยเหตุผลด้ านความปลอดภัย
เทคโนโลยี RIA ในปัจจุบนั
ในปั จจุบนั มีเทคโนโลยีหลายชนิดจากผู้พฒ
ั นาหลายราย ที่อาจนับได้ วา่ เป็ น RIA


AJAX
Adobe Flash, Adobe Flex และ Adobe AIR



OpenLaszlo
JavaFX



Microsoft Silverlight
ActiveX
XUL
จาวาแอพเพล็ต
JavaFX คือ
(แบบเดียวกับ Silverlight คือ Microsoft Flash)
JavaFX คือ Java Applet
ตลาด RIA นันต้
้ องยกให้ Java Applet ของซันเป็ นผู้บกุ เบิกเจ้ าแรกๆ
แต่อาจเป็ นได้ วา่ แนวคิดของซันนันล
้ ้าหน้ ายุคสมัยเกินไป หรื อไม่ก็ Java Applet ทางานได้ ช้าเกินไป เขียนยากเกินไป
ทาให้ ในภายหลังตลาด RIA นันถู
้ กตีตลาดด้ วย Flash Player จากค่าย Adobe/Macromedia
จนทุกวันนี ้แทบไม่มีใครพูดถึง Java Applet อีกแล้ ว สาเหตุที่โลกจาเป็ นต้ องมี RIA ก็เป็ นเพราะว่า HTML
และเว็บเบราว์เซอร์ นนมี
ั ้ ความสามารถไม่พอกับความต้ องการของแอพพลิเคชัน บางอย่าง (เช่น วิดโี อ, แอนิเมชัน หรื อ
กราฟฟิ กแบบเวกเตอร์ ) ถึงแม้ วา่ HTML5 จะแก้ ปัญหาเหล่านี ้ไปบ้ างบางจุด
แต่ก็ยงั ไม่ทนั ต่อความต้ องการของทังผู
้ ้ ใช้ และผู้พฒ
ั นาซอฟต์แวร์ (HTML4 ออกเมื่อปี 2000 ส่วน HTML5 ยังอีกนาน)
จุดนี ้จึงเป็ นช่องว่างให้ กบั บริษัทไอทียกั ษ์ ใหญ่ เข้ ามาสร้ างเทคโนโลยีที่แสดงผลเนื ้อหาแบบอินเทอร์ แอคทีฟออนไลน์
โลกของ RIA นันต่
้ างจากเว็บตรงที่ไม่มีหน่วยงานที่คอยคุมมาตรฐานอย่าง W3C คุม HTML ดังนันถ้
้ าใครชนะในตลาดนี ้
ก็แปลว่าตัวเองจะมีอิทธิพลขนาดกาหนดทิศทางของโลกอินเทอร์ เน็ตได้ สว่ นหนึง่ เลยทีเดียว
ผู้เล่นหลักในตลาดนี ้มี 3 ราย ได้ แก่ Flash/Flex จาก adobe, Silverlight จากไมโครซอฟท์ และล่าสุดคือ
JavaFX จากซัน
มีอะไรใหม่ ใน JavaFX
JavaFX คือ Java Applet ฟี เจอร์ ใหม่ (สาหรับผู้ใช้ ) มีดงั นี ้
 ระบบ GUI toolkit แบบใหม่ มาใช้ แทน Swing (แต่ก็ยงั ใช้ Swing ได้ ถ้าต้ องการ)
 สนับสนุนกราฟฟิ กแบบเวกเตอร์ (SVG) และแอนิเมชัน (ยังไม่มี 3D ในเวอร์ ชน
ั 1.0 แต่มีเอฟเฟคต์แบบเหมือน 3D
ให้ เรี ยกใช้ ) เร่งความเร็ วด้ วย GPU ได้
 เล่นวิดีโอด้ วย codec VP6 จากบริ ษัท On2 (เทียบเท่ากับ Flash แต่ใช้ ฟอร์ แมต FLM ซึง่ เป็ นซับเซ็ตของ FLV) และ
H.264 บนบางแพลตฟอร์ ม
 สนับสนุน XML, JSON
 การลากแอพเพล็ต JavaFX ออกมาจากเว็บเบราว์เซอร์ แล้ วรันต่อบนเดสก์ท็อปได้ ทน
ั ที
(ในอนาคตเราคงเห็นอะไรคล้ ายๆ กันนี ้จากฝั่ง Flash/AIR)
JavaFX ทำงำนอย่ ำงไร
คนที่เคยใช้ Flash และ Silverlight คงทราบดีวา่ ต้ องดาวน์โหลดตัวรันไทม์ (หรื อบางยี่ห้อจะเรี ยก Player)
มาติดตังในเครื
้
่ องเสียก่อน จึงจะสามารถเล่นเนื ้อหาทีเ่ ป็ น Flash หรื อ Silverlight ได้ สาหรับ JavaFX
นันจะต่
้ างออกไปเล็กน้ อย เนื่องจากว่ามันเป็ นจาวา JavaFX จะรันด้ วย Java Runtime Environment (JRE)
ที่ติดตังอยู
้ บ่ นคอมพิวเตอร์ อยูแ่ ล้ ว
แต่วา่ JavaFX นันมี
้ ฟีเจอร์ ใหม่ๆ บางอันที่ JRE รุ่นปกติไม่มี ดังนันในครั
้
ง้ แรกที่เรารันแอพเพล็ต JavaFX ตัว JRE
จะดาวน์โหลดส่วนขยายทีจ่ าเป็ นในการรัน JavaFX โดยอัตโนมัติ ในทางทฤษฎี มันก็คือการดาวน์โหลดรันไทม์ของ
JavaFX แบบเดียวกับ Silverlight หรื อ Flash นัน
่ เอง แต่ในทางปฏิบตั ิ ผู้ใช้ จะไม่ต้องผ่านกระบวนการดาวน์โหลด ติดตัง้
รี สตาร์ ทเว็บเบราว์เซอร์ ซึง่ ลดความยุง่ ยากลงไปได้ พอสมควร ซันหวังว่า การที่คอมพิวเตอร์ จานวนมากมี JRE
ติดตังอยู
้ แ่ ล้ ว จะช่วยให้ JavaFX นันเป็
้ นที่นิยมได้ งา่ ยขึ ้น มีคอมพิวเตอร์ ที่สามารถรัน JavaFX
จานวนมากได้ ในระยะเวลาอันรวดเร็ ว เพราะว่าสงคราม RIA นี ้เป็ นสงครามชิงพื ้นที่ ใครเผยแพร่รันไทม์ได้ มากกว่า
คนนันมี
้ โอกาสชนะสูง (ปั จจุบนั adobe อ้ างว่าคอมพิวเตอร์ 98% นันติ
้ ดตัง้ Flash Player)
NetBeansTM IDE 3.6 Installation Guide
Windows
On Microsoft Windows platforms, you can pick from one of the following downloads:
 Self-extracting installer
 Archive distribution
Using the Windows Installer
Once you have downloaded the installer file, double-click the file to start the installation wizard. The
installer enables you to specify:
 the Java™ 2 Software Development Kit to run the IDE on.(JDK)
 which directory to install NetBeans IDE into.
 whether to associate .java and .nbm (NetBeans module files) with the IDE.
To launch NetBeans IDE, do one of the following:
 Double-click the NetBeans IDE icon on your desktop.
 Select NetBeans IDE | NetBeans IDE 3.6 | NetBeans IDE from the Start menu.
Using Archive Installations on Windows Machines
While the installer described above is the preferred method of installation on Microsoft Windows
machines, you can also install NetBeans IDE using a tar.gz or zip file. Common archive utilities like
Winzip can work with both of these file types.
To install NetBeans IDE:
1. Locate the archive that you have downloaded and double-click it to open your archive file
tool.
2. Using your archive tool, extract all files to an empty directory, such as C:\NetBeans IDE 3.6.
To launch NetBeans IDE:
1. Navigate to the bin subdirectory of your NetBeans IDE installation.
2. Double-click runide.exe to start NetBeans IDE.
Run java development kit (JDK)
Run netbeans setup
Start netbeans
Setting Up the Project
To create an IDE project:
1. Start NetBeans IDE.
2. In the IDE, choose File > New Project (Ctrl-Shift-N), as shown in the figure below.
3. In the New Project wizard, expand the Java category and select Java Application as shown in
the figure below. Then click Next.
4. In the Name and Location page of the wizard, do the following (as shown in the figure
below):
o
In the Project Name field, type HelloWorldApp.
o
Leave the Use Dedicated Folder for Storing Libraries checkbox unselected.
o
In the Create Main Class field, type helloworldapp.HelloWorldApp.
o
Leave the Set as Main Project checkbox selected.
5. Click Finish.
The project is created and opened in the IDE. You should see the following components:

The Projects window, which contains a tree view of the components of the project, including
source files, libraries that your code depends on, and so on.

The Source Editor window with a file called HelloWorldApp open.

The Navigator window, which you can use to quickly navigate between elements within the
selected class.

The Tasks window, which lists compilation errors as well other tasks that are marked with
keywords such as XXX and TODO.
Adding Code to the Generated Source File
Because you have left the Create Main Class checkbox selected in the New Project wizard, the IDE
has created a skeleton main class for you. You can add the "Hello World!" message to the skeleton
code by replacing the line:
// TODO code application logic here
with the line:
System.out.println("Hello World!");
Save the change by choosing File > Save.
The file should look something like the following code sample.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package helloworldapp;
/**
*
* @author <your name>
*/
public class HelloWorldApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Compiling and Running the Program
Because of the IDE's Compile on Save feature, you do not have to manually compile your project in
order to run it in the IDE. When you save a Java source file, the IDE automatically compiles it.
The Compile on Save feature can be turned off in the Project Properties window. Right-click your
project, select Properties. In the Properties window, choose the Compiling tab. The Compile on Save
checkbox is right at the top. Note that in the Project Properties window you can configure numerous
settings for your project: project libraries, packaging, building, running, etc.
To run the program:

Choose Run > Run Main Project (F6).
The next figure shows what you should now see.
Congratulations! Your program works!
If there are compilation errors, they are marked with red glyphs in the left and right margins of the
Source Editor. The glyphs in the left margin indicate errors for the corresponding lines. The glyphs in
the right margin show all of the areas of the file that have errors, including errors in lines that are
not visible. You can mouse over an error mark to get a description of the error. You can click a glyph
in the right margin to jump to the line with the error.
Building and Deploying the Application
Once you have written and test run your application, you can use the Clean and Build command to
build your application for deployment. When you use the Clean and Build command, the IDE runs a
build script that performs the following tasks:

Deletes any previously compiled files and other build outputs.

Recompiles the application and builds a JAR file containing the compiled files.
To build your application:

Choose Run > Clean and Build Main Project (Shift-F11)
You can view the build outputs by opening the Files window and expanding the HelloWorldApp
node. The compiled bytecode file HelloWorldApp.class is within the build/classes/helloworldapp
subnode. A deployable JAR file that contains the HelloWorldApp.class is within the dist node.
Developing General Java Applications
The following short tutorial takes you through some of the basic steps of developing a Java SE
application in NetBeans IDE. This tutorial assumes you already have some familiarity with developing
Java applications. Along the way, you will see some of the IDE features that simplify application
development.
You will create an application that converts several words into a single word that contains one letter
from each of the other words. The resulting word is called an acrostic.
This tutorial takes approximately 30 minutes to complete. If you would like to do a quicker "Hello
World" tutorial, see the NetBeans IDE Java Quick Start Tutorial.
Contents

Project Setup

Creating and Editing Java Source Code

Compiling and Running the Application

Testing and Debugging the Application

Building, Running, and Distributing the Application

Other Common Tasks

Next Steps
To complete this tutorial, you need the software and resources listeds in the following table.
Software or Resource
Version Required
NetBeans IDE
version 6.9 or later
Java Development Kit (JDK) version 6 or version 7
Project Setup
The application you create will contain two projects:

A Java Class Library project in which you will create a utility class.

A Java Application project with a main class that implements a method from the library
project's utility class.
After you create the projects, you will add the library project to the classpath of the application
project. Then you will code the application. The library project will contain a utility class with an
acrostic method. The acrostic method takes an array of words as a parameter and then generates an
acrostic based on those words. The MyApp project will contain a main class that calls the acrostic
method and passes the words that are entered as arguments when the application is run.
Note: Strictly speaking, two projects are not needed for such a simple application. This tutorial uses
two projects to demonstrate features that you might need for a more complex application.
Creating a Java Class Library Project
1. Choose File > New Project (Ctrl-Shift-N). Under Categories, select Java. Under Projects, select
Java Class Library. Click Next.
2. Under Project Name, type MyLib. Change the Project Location to any directory on your
computer. From now on, this tutorial refers to this directory as NetBeansProjects.
Note: The path specified above should appear as follows in the Project Folder field of the wizard:
/NetBeansProjects/MyLib/
3. (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.
4. Click Finish. The MyLib project opens in both the Projects window and the Files window.
Creating a Java Application Project
1. Choose File > New Project. Under Categories, select Java. Under Projects, select Java
Application. Click Next.
2. Under Project Name, type MyApp. Make sure the Project Location is set to
NetBeansProjects.
3. (Optional) Check the Use Dedicated Folder for Storing Libraries checkbox.
4. Enter acrostic.Main as the main class.
5. Ensure that the Set as Main Project and Create Main Class checkboxes are checked.
6. Click Finish. The MyApp project is displayed in the Project window and Main.java opens in
the Source Editor.
Configuring the Compilation Classpath
Since MyApp is going to depend on a class in MyLib, you have to add MyLib to the classpath of
MyApp. Doing so also ensures that classes in the MyApp project can refer to classes in the MyLib
project without causing compilation errors. In addition, this enables you to use code completion in
the MyApp project to fill in code based on the MyLib project. In the IDE, the classpath is visually
represented by the Libraries node.
To add the library's utility classes to the project classpath:
1. In the Projects window, right-click the Libraries node for the MyApp project and choose Add
Project as shown in the image below.
2. Browse to NetBeansProjects/ and select the MyLib project folder. The Project JAR Files pane
shows the JAR files that can be added to the project. Notice that a JAR file for MyLib is listed
even though you have not actually built the JAR file yet. This JAR file will get built when you
build and run the MyApp project.
3. Click Add Project JAR Files.
4. Expand the Libraries node. The MyLib project's JAR file is added to the MyApp project's
classpath.
Creating and Editing Java Source Code
Now you need to create a Java package and add the method that you will use to construct the
acrostic. After that you need to implement the acrostic method in the Main class.
Creating a Java Package and Class File
1. Right-click the MyLib project node and choose New > Java Class. Type LibClass as the name
for the new class, type org.me.mylib in the Package field, and click Finish. LibClass.java opens
in the Source Editor.
2. In LibClass.java, place the cursor on the line after the class declaration (public class LibClass
{.
3. Type or paste in the following method code:
4.
public static String acrostic(String[] args) {
5.
StringBuffer b = new StringBuffer();
6.
for (int i = 0; i < args.length; i++) {
7.
8.
9.
if (args[i].length() > i) {
b.append(args[i].charAt(i));
} else {
10.
b.append('?');
11.
}
12.
}
13.
return b.toString();
}
14. If the code that you pasted in is not formatted correctly, press Alt-Shift-F to reformat the
entire file.
15. Press Ctrl-S to save the file.
Editing a Java File
Now you will add some code to Main.java. In doing so, you will see the Source Editor's code
completion and code template (abbreviation) features.
1. Select the Main.java tab in the Source Editor. If it isn't already open, expand MyApp > Source
Packages > acrostic in the Projects window and double-click Main.java.
2. Delete the // TODO code application logic here comment in the main method.
3. In place of the comment type the following:
String result = Li
Leave the cursor immediately after Li. In the next step you will use code completion to turn Li into
LibClass.
4. Press Ctrl-Space to open the code completion box.
A short list of possible ways to complete the word appears. However, the class that you want,
LibClass might not be there.
5. Press Ctrl-Space again to display a longer list of possible matches.
LibClass should be in this list.
6. Select LibClass and press Enter. The IDE fills in the rest of the class name and also
automatically creates an import statement for the class.
Note: The IDE also opens a box above the code completion box that displays Javadoc information for
the selected class or package. Since there is no Javadoc information for this package, the box
displays a "Cannot find Javadoc" message.
7. In the main method, type a period (.) after LibClass. The code completion box opens again.
8. Select the acrostic(String[]args) method and press Enter. The IDE fills in the acrostic method
and the highlights the args parameter.
9. Press Enter to accept args as the parameter.
10. Type a semicolon (;).
The final line should look like the following line.
String result = LibClass.acrostic(args);
11. Press Enter to start a new line. Then type sout and press Tab. The sout abbreviation expands
to System.out.println(""); with the cursor positioned between the quotation marks. Type
Result = inside the quotation marks and + result after the end quotation mark.
The final line should look like the following line.
System.out.println("Result = " + result);
12. Press Ctrl-S to save the file.
Note: sout is one of many code templates that are available in the Source Editor. To find and edit the
list of code templates, choose Tools > Options > Editor > Code Template.
Compiling and Running the Application
Now you need to set the main class and execution arguments so that you can run the project.
Note: By default, the projects have been created with the Compile on Save feature enabled, so you
do not need to compile your code first in order to run the application in the IDE. For more
information on the Compile on Save feature, see the Compile on Save section of the Creating,
Importing, and Configuring Java Projects guide.
Setting the Main Class and Execution Arguments
The output of this program is based on arguments that you provide when you run the program. As
arguments, you will provide five words, from which the acrostic "Hello" will be generated. The
acrostic is assembled from the first letter of the first word, the second letter of the second word, the
third letter of the third word, and so on.
To add the arguments for the IDE to use when running the application:
1. Right-click the MyApp project node, choose Properties, and select the Run node in the
dialog's left pane.
The main class should already be set to acrostic.Main.
2. Type However we all feel zealous in the Arguments field and click OK.
Running the Application
Now that you have created the application and provided runtime arguments for the application, you
can test run the application in the IDE.
To run the application in the IDE:
1. Right-click the MyApp project node and choose Clean and Build.
2. Choose Run > Run Main Project (F6).
In the Output window, you should see the output from the program, Result = Hello (the acrostic of
the phrase that was passed to the program as an argument).
Testing and Debugging the Application
Now you will create and run a test for the project using JUnit and then run the application in the
IDE's debugger to check for errors. In the JUnit test, you will test the LibClass by passing a phrase to
the acrostic method and using an assertion to indicate what you think the result should be.
Creating JUnit Tests
1. Right-click the LibClass.java node in the Projects window and choose Tools >Create JUnit
Tests (Ctrl-Shift-U).
If this is the first time you have created JUnit tests in the IDE, you will be prompted with the Select
JUnit Version dialog box. Press Enter to select JUnit 4.x and continue to the Create Tests dialog box.
2. In the Create Tests dialog box, click OK to run the command with the default options. The
IDE creates the org.me.mylib package and the LibClassTest.java file in a separate test folder.
You can find this file by expanding the Test Packages node and the org.me.mylib subnode.
3. In LibClassTest.java, delete the body of the public void testAcrostic() method.
4. In place of the deleted lines, type or paste in the following:
5. System.err.println("Running testAcrostic...");
6. String result = LibClass.acrostic(new String[]
7.
{"fnord", "polly", "tropism"});
assertEquals("Correct value", "foo", result);
8. Save the file by pressing Ctrl-S.
Running JUnit Tests
1. Select the MyLib project node and choose Run > Test Project (MyLib) or press Alt-F6. The
MyLib (test) tab opens in the Output window. The JUnit test cases are compiled and run. The
JUnit test result shows that the test passes.
2. You can also run a single test file rather than testing the entire project. Select the
LibClass.java tab in the Source Editor and choose Run > Test File.
The JUnit API documentation is available from the IDE. Choose Help > Javadoc References > JUnit
VersionNumber.
You can learn more about JUnit by visiting http://www.junit.org
Debugging the Application
In this section, you will use the debugger to step through the application and watch the values of
variables change as the acrostic is assembled.
To run the application in the debugger:
1. In the LibClass.java file, go to the acrostic method and place the insertion point anywhere
inside b.append(args[i].charAt(i));. Then press Ctrl-F8 to set a breakpoint.
2. Choose Debug > Debug Main Project (Ctrl-F5). The IDE opens the Debugger windows and
runs the project in the debugger until the breakpoint is reached.
3. Select the Local Variables window in the bottom of the IDE and expand the args node. The
array of strings contains the phrase you entered as the command arguments.
4. Press F7 (or choose Debug > Step Into) to step through the program and watch the b
variable change as the acrostic is constructed.
When the program reaches the end, the debugger windows close.
For more information, see Writing JUnit Tests in NetBeans IDE.
Building, Running, and Distributing the Application
Once you are satisfied that your application works properly, you can prepare the application for
deployment outside of the IDE. In this section you will build the application's JAR file and then run
the JAR file from the command line.
Building the Application
The main build command in the IDE is the Clean and Build command. The Clean and Build command
deletes previously compiled classes and other build artifacts and then rebuilds the entire project
from scratch.
Notes: There is also a Build command, which does not delete old build artifacts, but this command is
disabled by default. See the Compile on Save section of the Creating, Importing, and Configuring Java
Projects guide for more information.
To build the application:

Choose Run > Clean and Build Main Project (Shift-F11).
Output from the Ant build script appears in the Output window. If the Output window does not
appear, you can open it manually by choosing Window > Output > Output.
When you clean and build your project, the following things occur:

Output folders that have been generated by previous build actions are deleted ("cleaned").
(In most cases, these are the build and dist folders.)

build and dist folders are added to your project folder (hereafter referred to as the
PROJECT_HOME folder). You can view these folders in the Files window.

All of the sources are compiled into .class files, which are placed into the
PROJECT_HOME/build folder.

A JAR file containing your project is created inside the PROJECT_HOME/dist folder.

If you have specified any libraries for the project (in addition to the JDK), a lib folder is
created in the dist folder. The libraries are copied into dist/lib.

The manifest file in the JAR is updated to include entries that designate the main class and
any libraries that are on the project's classpath.
Note: You can view the contents of the manifest in the IDE's Files window. After you have built your
project, switch to the Files window and navigate to dist/MyApp.jar. Expand the node for the JAR file,
expand the META-INF folder, and double-click MANIFEST.MF to display the manifest in the Source
Editor.
Main-Class: acrostic.Main
Class-Path: lib/MyLib.jar
(To find more about manifest files, you can read this chapter from the Java Tutorial.)
Running the Application Outside of the IDE
To run the application outside of the IDE:
1. On your system, open up a command prompt or terminal window.
2. In the command prompt, change directories to the MyApp/dist directory.
3. At the command line, type the following statement:
java -jar MyApp.jar However we all feel zealous
The application then executes and returns the following output as shown in the image below:
Result = Hello
Distributing the Application to Other Users
Now that you have verified that the application works outside of the IDE, you are ready to distribute
the application.
To distribute the application:
1. On your system, create a zip file that contains the application JAR file (MyApp.jar) and the
accompanying lib folder that contains MyLib.jar.
2. Send the file to the people who will use the application. Instruct them to unpack the zip file,
making sure that the MyApp.jar file and the lib folder are in the same folder.
3. Instruct the users to follow the steps in the Running the Application Outside of the IDE
section above.
Other Common Tasks
You have now completed the main part of the tutorial, but there are still some basic tasks that have
not been covered. This section includes a few of those tasks.
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.
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 Close.
Generating Javadoc for a Project
You can generate compiled Javadoc documentation for your project based on Javadoc comments
that you have added to your classes.
To generate Javadoc documentation for a project:
1. Select the MyLib project.
2. Choose Run > Generate Javadoc for "MyLib" from the IDE's main menu.
The generated Javadoc is added to the dist folder of the project. In addition, the IDE opens a
web browser that displays the Javadoc.
--------------------------------------------------------------end-----------------------------------------------------------------
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 IDEgenerated 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 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
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 7.1
Java Development Kit (JDK) version 6 or higher
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 on Windows/Cmd-Shift-N on Mac OS X).
When the New Project wizard appears, select the right template for your project and complete the
remaining wizard steps. The project template icon can be displayed in gray, which means that this
project type has not been activated. Proceed with creating the project and this functionality will be
activated in the IDE.
The IDE contains the following standard project templates for Java and Java Web applications:

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 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 FreeForm 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 Project Properties dialog box. The Projects window can be
opened by choosing Window > Projects (Ctrl-1 on Windows/Cmd-1 on Mac OS X).

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 on
Windows/Cmd-2 on Mac OS X).
Note: If you need to access files and directories that are outside of your project directories, you can
use the Favorites window. You open the Favorites window by choosing Window > Favorites (Ctrl-3
on Windows/Cmd-3 on Mac OS X). You add a folder or file to the Favorites window by right-clicking
in the Favorites window and choosing Add to Favorites.
Importing a Project
This section shows you how to handle the initial importing of projects into the IDE.
Importing an Eclipse Workspace
For Eclipse projects, you can use the Import Eclipse Project wizard to help you create NetBeans
projects from projects in an Eclipse workspace and import the project classpaths and other settings.
When you use the Import Eclipse Project wizard, you do not need to use create and configure the
NetBeans project manually. Open the wizard by choosing File > Import Project > Eclipse Project. See
Importing Eclipse Projects into NetBeans IDE for information on how the wizard works and how you
can work simultaneously in Eclipse and NetBeans projects.
Setting Up a Java Project Based on Existing Sources
For other Java projects developed outside of NetBeans, you use an "Existing Sources" template in
the New Project wizard to make a NetBeans project. In the wizard, you identify the location of the
sources and specify a location for the NetBeans project metadata. You then use the Project
Properties dialog box to configure the project.
To set up a NetBeans project for an existing Java application:
1. Choose File > New Project (Ctrl-Shift-N on Windows/Cmd-Shift-N on Mac OS X).
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.
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 on Windows/Cmd-Shift-N on Mac OS X).
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. See Sharing Project Libraries in NetBeans IDE for more
information on this option.
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. Set the source level to the Java version on which you want the application to run.
10. (Optional) Adjust the context path. By default, the context path is based on the project
name.
11. Click Next to advance to the Existing Sources and Libraries page of the wizard.
12. Verify all of the fields on the page, such as the values for the Web Pages Folder and Source
Package Folders.
13. 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.
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.
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 Classpath
Adding a group of class files to a project's classpath tells the IDE which classes the project should
have access to during compilation and execution. The IDE also uses classpath settings to enable code
completion, automatic highlighting of compilation errors, and refactoring. You can edit the classpath
declarations for an existing project in the Project Properties dialog box.
In standard Java projects, the IDE maintains separate classpaths for compiling and running your
project, as well as compiling and running JUnit tests (for Java SE applications). The IDE automatically
adds everything on your project's compilation classpath to the project's runtime classpath.
You can modify your project's classpath in either of the following ways:

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.
Managing Dependencies Between Projects
If your application consists of multiple projects, you have to set up the classpath dependencies
between the projects. Typically you set up one main project containing the project main class (in
Java SE projects), and several required projects. A required project is a project that has been added
to another project's classpath. When you clean and build a project, the IDE also cleans and builds its
required projects. The required project's Javadoc and sources are also made available to the
receiving project.
You can add any required projects to your project by right-clicking the Libraries node in the Projects
window, or you can add them by specifying them in the Libraries tab in the Project Properties dialog
box. When you want to add a required project, select the project folder whose JAR files you want to
add to the classpath (the file chooser displays the icon for IDE project folders (
). When adding
projects in the Project Properties dialog box, make sure that the Build Projects on Classpath
checkbox is selected.
If you want to add a free-form project to the classpath of a standard project, you have to add the
free-form project's JAR file to the standard project's classpath. To do this, you must first declare all
of the free-form project's output files in the Output panel of the free-form project's Project
Properties dialog box.
Sharing Project Libraries
As the NetBeans IDE project system is Ant-based, NetBeans projects are already generally portable
between different users, whether or not they use the IDE. However, by default, the project build
script's way of referring to libraries is dependent on factors specific to each user, particularly in the
case of libraries defined in the Library Manager dialog box.
For example, normally you can put a project in a zip file and give it to another user to unpack and
run. But if the project depends on a custom library that you have specified in the Libraries Manager
dialog box, others might initially have problems building the project because of unresolved
references to the custom library.
Another potential inconvenience is that libraries are stored in a variety of locations, based on where
they originate. Libraries that come with the IDE are stored in various different folders within the
IDE's installation. Examples of these include the libraries for the Swing Layout Extensions, beans
binding, and database drivers.
To solve these problems, you can specify a dedicated folder for project libraries. Furthermore, you
have control over whether to use an relative path or an absolute path when referring to these
libraries.
This flexibility makes it easier for you to handle the following situations:

You 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 where project 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.
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's nblibraries.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's nblibraries.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.
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 Application
You can configure your application to run through Java Web Start in the IDE. For this, use the Java
Web Start category in the Project Properties window. For detailed information on how to enable
Java Web Start, refer to the tutorial Enabling Java Web Start in the NetBeans IDE.
To configure Java Web Start for the application:
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 Applications
This section explains how to build standard projects in the IDE, how to do basic customizations to
the build process, and how to handle compilation errors.
Building Projects, Packages, and Files
Compilation in the IDE is simple. Assuming that your project's compilation classpath is set correctly,
you need only select the project, package, or file that you want to compile and choose the
appropriate Build or Compile command from the Run menu. The IDE then compiles the files.
If the Compile on Save feature is enabled for a Java project (or the Deploy on Save feature is enabled
for a Java web or enterprise project), the IDE compiles your files automatically when you save them.
See Compile on Save below for more information.
To build your application for deployment:

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.

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 multitabbed window is displayed automatically whenever you generate compilation errors, debug your
program, generate Javadoc documentation, and so on. You can also open this window manually by
choosing Window > Output (Ctrl-4 on Windows/Cmd-4 on Mac OS X).
In addition, compilation errors are shown in the Tasks window and marked in the Source Editor.
One important function of the Output window is to notify you of errors found while compiling your
program. The error message is displayed in blue underlined text and is linked to the line in the
source code that caused the error, as illustrated in the image below. The Output window also
provides links to errors found when running Ant build scripts. Whenever you click an error link in the
Output window, the Source Editor jumps to the line containing the error automatically.
Every action that is run by an Ant script, such as compiling, running, and debugging files, sends its
output to the same Output window tab. If you need to save the messages displayed in the Output
window, you can copy and paste it to a separate file. You can also set Ant to print the command
output for each new target to a new Output window tab by choosing Tools > Options, selecting the
Miscellaneous category, clicking the Ant tab, and deselecting the checkbox for the Reuse Output
Tabs from Finished Processes property.
Filtering Output Files
When you create a JAR file or a WAR file, you usually want to include just the compiled .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
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
that have been created when you have saved those files. The Ant build script is not used. If you have
defined custom steps in the build script, those steps are not followed. If you would like the full build
process to occur when you use Run Project, Debug Project, and Profile Project, disable Compile on
Save.
Customizing Runtime Options
By default, the IDE does not specify a main class, runtime arguments, or JVM arguments. The
runtime classpath of each standard project contains the project's compiled classes and everything in
the project's compilation classpath. You can view the project's compilation classpath by opening the
Project Properties dialog box and selecting the Libraries node in the Categories pane and then
clicking the Compile tab in the right pane.
To change project runtime options, open the Project Properties dialog box by right-clicking the
project node in the Projects window and choosing Properties. Next, select the Libraries node in the
Categories pane and click the Run tab in the right pane of the dialog box. Note that to access settings
for the main class, program arguments, the working directory for program execution and VM
options, you have to select the Run node. In the next section we'll take a closer look at how to
configure the runtime classpath.
Setting the Runtime Classpath
To add projects, libraries, JAR files, and folders to the project's runtime classpath, use the buttons on
the right side of the Run-time Libraries list in the Project Properties dialog box.
If your project uses special libraries dynamically at runtime through an indirect interface or
reflection (like JDBC drivers or JAXP implementations), you have to add these libraries to the runtime
classpath. You also have to adjust your runtime classpath if the runtime dependencies between your
projects do not match the compilation dependencies between the projects. For example, imagine
that project A compiles against project B, and project B compiles against project C, but project A
does not compile against project C. This means that project A only has project B on its runtime
classpath. If project A requires both project B and project C during execution, you have to add
project C to project A's runtime classpath.
Setting the Main Class and Runtime Arguments
To set the project's main class, select the Run node in the Categories pane of the Project Properties
dialog box and type the fully-qualified name in the Main Class field (for example,
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.
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/buildimpl.xml, which contains build targets that are generated by the IDE. In build.xml, you can override
any of the targets from nbproject/build-impl.xml or write new targets. Do not edit nbproject/buildimpl.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 time you 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
o
<target name="-post-compile">
<rmic base="${build.classes.dir}" includes="**/Remote*.class"/>
</target>
o
Change the instructions in an Ant target. Copy the target from nbproject/buildimpl.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:
o
<target name="new-target">
o
o
<!-- target body... -->
</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
task
Follow these steps
Right-click the project node in the Projects window and choose Properties. Click
Specify which files
the Packaging subnode (under Build) and configure the filter and compression
are added to a JAR
settings using the Exclude from JAR File field. For more, see Filtering Output
file.
Files
In the Files window, double-click the project's nbproject/project.properties file
Change a JAR file's
to open it in the Source Editor. Enter the full path to the JAR file in the dist.jar
name and location.
property.
Specify the
manifest 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
In the Files window, open your project folder and open build.xml. Override the
generation of a JAR jar target to have no contents and no dependencies. For example, add the
file for a project.
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.
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 description attribute, 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.
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.
Download