This document takes you through the basics of using NetBeans IDE 3.6 by creating
a simple Hello World application. This document is designed to get you going
as quickly as possible. If you want a more detailed description of NetBeans
IDE features, see the following documents:
Task |
Description |
Create a new project |
- Choose Project > Project Manager (Ctrl-Shift-N).
- Click New, give the project a name, and click OK.
|
Add a source directory
|
- Choose File > Mount Filesystem.
- Select the Local Directory node in the wizard that appears and click
Next.
- On Select Items to Mount page of the wizard, create a folder on your
system called src.
Note: If you are on a Windows system, you cannot create a new folder
in your My Documents folder.
- Select the folder and click Finish.
The src filesystem node appears in the Filesystems window.
The filesystem's sources are added to the project classpath.
Note: Don't navigate into the src directory. Just select the src
directory and click Finish.
|
Create a new package |
- Right-click the src directory node in the Filesystems window and
choose New > Java Package.
- In the name field, type hello and click Finish.
|
Create a new file |
- Right-click the hello package's node in the Filesystems window and choose
New > Java Main Class.
- In the name field, type HelloWorld and click Finish.
|
Task |
Description |
Introduction |
In this section, we will write the code for the HelloWorld application.
We will use the following tools:
- Code completion. The Source Editor parses all of the packages and
classes in the classpath, including the standard JDK classes, and automatically
suggests matching packages and classes as you type.
- Abbreviations. The IDE includes abbreviations for many commonly used
word combinations that expand to the full words when you press Space.
For example, typing psf and pressing Space expands the abbreviation
to public static final.
- Fast Import. The Source Editor lets you automatically generate the
import statement for any class.
- Automatic formatting and error highlighting.
When you are done with this section, HelloWorld.java will look
like this:
/*
* HelloWorld.java
*
* Created on 16 November 2004, 11:52
*/
package hello;
import java.util.Date;
/**
*
* @author me :-)
*/
public class HelloWorld {
/** Creates a new instance of HelloWorld */
public HelloWorld() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String date = new Date().toString();
System.out.println("Hello world! It's " + date);
}
}
|
Use code completion |
- Place the cursor at the end of the public static void main(String[]
args) { line and press Enter.
- Type String date = new Dat and wait. Within a second or two,
the code completion box should appear, listing all of the JDK classes
that begin with Dat. You can also manually open the code completion
box by pressing Ctrl-Space.
The Source Editor also shows the Javadoc description of each class,
if available. The IDE extracts this description from the Javadoc comments
in the source code. For more information on working with Javadoc documentation,
see the Using
NetBeans IDE guide.
- In the code completion box, scroll down to Date. Notice there
are two Date classes in the classpath. Select either of the
two and press Enter. - u
Note: Unless you use a fully-qualified name for a class, the code
completion box offers the methods of all classes with the same name.
It therefore does not matter whether you choose java.sql.Date
or java.util.Date from the code completion box in the step
above.
- Type (. Notice the IDE automatically fills in the closing
parentheses.
- Go to the end of the line and type a . after the closing
parentheses. The code completions box opens listing all of the methods
in the Date class. Deprecated methods are shown with a line
crossed through them. Start typing toString to narrow the selection
down, then select toString and press Enter.
- Type a closing ;.
|
View errors |
- Notice that the line you just entered is underlined with a red line
and has a
icon in the left margin, meaning that it contains errors.
- Hold the mouse over the icon to view the error. Since you have not
entered an import statement for the Date class, the file contains a
cannot resolve symbol compilation error.
|
Create import statement
|
- Place the insertion point anywhere in the word Date and press
Alt-Shift-I.
- In the Fast Import dialog box, choose java.util.Date and
click OK. The import statement is created and the Source Editor no longer
shows an error for the line.
|
Expand abbreviations
|
- Place the cursor at the end of the String date = new Date().toString();
line and press Enter.
- Type sout and press Space. The sout abbreviation
expands to System.out.println("");
- Type Hello World! It's between the quotation marks (be sure
to include a space after It's).
- After the closing quotation mark, type + date.
|
Reformat code
|
- If the spacing for the comment line does not match the spacing for
the rest of the file, press Ctrl-Shift-F to reformat the entire file.
If any lines are selected when you press Ctrl-Shift-F, only those lines
are reformatted.
|
Task |
Description |
Add a breakpoint to a file
|
- Click in the left margin (Shift-F8) of the file on the System.out.println("Hello
World! It's " + date); line.
A red breakpoint icon appears in the margin and the line is highlighted.
|
Add a watch to a variable
|
- Right-click the word date in the String date = new Date().toString();
line and choose New Watch (Ctrl-Shift-F7).
- Make sure that the Watch Expression field contains date (with
a lower case d) and click OK in the dialog box.
|
Start a debugging session |
- Choose Debug > Start Session > Run in Debugger (Alt-F5).
The IDE runs the file until the breakpoint is reached. Use the debugger
views at the bottom of the IDE to monitor your program's state, such
as the value of the date variable (listed under both Local
Variables and Watches).
|
Step through program execution |
- Use Step Into (F7) and Step Over (F8) to execute the program one line
at a time.
|
Stop a debugging session |
- Choose Debug > Finish Sessions (Shift-F5).
|