Creating a Sample Program

The first time you run NetBeans, you'll be asked several questions about how to configure the software. To run the program, double-click the NetBeans IDE icon on the desktop or find it on the Start menu:

  • On Windows XP, choose Start, All Programs, open the NetBeans 4.1 folder, then click NetBeans IDE.
  • On other Windows systems, choose Start, Programs, open the NetBeans 4.1 folder, then click NetBeans IDE.

If you have never used an integrated development environment before, you're likely to be a little shell-shocked when you see NetBeans for the first time. There are numerous menu commands, toolbar buttons, and other interface components at your disposal, as shown in Screenshot C.2.

Screenshot C.2. Viewing the NetBeans graphical user interface.

[View full size image]Java ScreenShot

Most IDEs are designed to make experienced programmers more productive rather than to help new programmers learn a language. NetBeans is no exception. It can be difficult to learn how to use an IDE at the same time you are learning Java. This is probably the biggest selling point for the Java Development Kit. However, if you want to use the power and convenience of NetBeans while learning Java, this section and some supplementary reading should be enough to get started. In this section, you create a short Java app and run it, displaying the output of the program. NetBeans organizes Java programs into projects:

Choose File, New Project. A template wizard opens, as shown in Screenshot C.3. All files are created from templates in NetBeans. There are templates for Java class files, JavaBeans, graphical user interface components, and other kinds of files.

Screenshot C.3. Choosing a template for a new Java class.

[View full size image]Java ScreenShot

Choose the General folder icon in the Categories pane to see the class templates that are available.

The Java app template is suitable for the projects you will undertake in this tutorial. It creates a simple Java class that can be run as an app. Choose its icon in the Projects pane and click Next.

The wizard asks for a name to give the class and the folder where it should be saved.

Type the name HelloUser in the Project Name field.

In the Project Location pane, if you don't like the folder NetBeans has designated for all coding projects, type a different folder.

Leave the other values alone and click Finish.

When you create your first project, NetBeans scans your computer for Java classes, even looking inside .jar files that hold class libraries in the Java Archive format. When the scan completes, NetBeans creates the source code for a Java app called HelloUser and opens it for editing in an editor window (see Screenshot C.4).

Screenshot C.4. Editing source code in NetBeans.

[View full size image]Java ScreenShot

All Java programs begin as source code—a series of statements created using a text editor and saved as a text file. The NetBeans editor keeps track of the current position of the cursor in a bar that's circled in Screenshot C.4. The first number is the line, counting down from 1 at the top of the file, and the second number is the character position on that line, counting from left to right. When you create the HelloUser project, it contains the following Java statements as lines 26–28:

public static void main(String[] args) {
 // TODO code app logic here

Delete the // TODO code app logic here comment and as a replacement, enter the following statements at that place in the file:

String username = System.getProperty("");
System.out.println("Hello, " + username);

When you're finished, lines 26–29 should be the same as Listing C.1. Make sure that all the parentheses, braces, and quotation marks in the listing are entered correctly and capitalize everything in the program exactly as shown.

Listing C.1. Partial Source Code of
26: public static void main(String[] args) {
27: String username = System.getProperty("");
28: System.out.println("Hello, " + username);
29: }

The line numbers and colons along the left side of Listing C.1 are not part of the program—they're included so that I can refer to specific lines by number in this tutorial. If you're ever unsure about the source code of a program in this tutorial, you can compare it to a copy on the tutorial's official World Wide Web site at the following address:

After you finish typing in these lines, save the project by choosing File, Save from the menu.

Compiling and Running the Program

When you save a source code file, NetBeans stores it in the selected folder and displays the file in the Files pane inside that folder. (Choose Windows, Files to see this pane.) The file must be compiled into Java bytecode before it can be used: Choose Build, Build Main Project. NetBeans attempts to compile the file, opening an Output pane to display the results of the attempt (see Screenshot C.5).

Screenshot C.5. Successfully compiling a Java app.

[View full size image]Java ScreenShot

If it compiles successfully, the message "BUILD SUCCESSFUL" will be displayed in the Output pane. If it fails, one or more error messages are displayed. To run the program after a successful compilation, choose Run, Run Main Project. The Output pane displays the output of the program: the text "Hello," followed by your username. Screenshot C.6 displays the output produced on my system.

Screenshot C.6. Running a Java app.

[View full size image]Java ScreenShot

That's all the skills you need to create simple Java apps and other classes with NetBeans. The integrated development environment can be used to accomplish a lot more, but it will be easier to master the software after you have developed some experience with the Java language by completing the 24 hours of this tutorial.

Did you Know?

Oracle offers a tutorial for beginning NetBeans users in the online help feature of the software: Choose Help, Help Contents; then double-click the Getting Started folder in the table of contents.

This tutorial also has a website where you can find solutions to problems, corrections, answers to reader questions, and other useful material. The site is available at