Previous Section  < Day Day Up >  Next Section

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 Figure C.2.

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


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 application and run it, displaying the output of the program.

NetBeans organizes Java programs into projects:

1.
Choose File, New Project. A template wizard opens, as shown in Figure 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.

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


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

3.
The Java Application template is suitable for the projects you will undertake in this book. It creates a simple Java class that can be run as an application. 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.

4.
Type the name HelloUser in the Project Name field.

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

6.
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 application called HelloUser and opens it for editing in an editor window (see Figure C.4).

Figure C.4. Editing source code in NetBeans.


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 Figure 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 application logic here

}


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


String username = System.getProperty("user.name");

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 HelloUser.java

26:     public static void main(String[] args) {

27:         String username = System.getProperty("user.name");

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 book. If you're ever unsure about the source code of a program in this book, you can compare it to a copy on the book's official World Wide Web site at the following address:


http://www.java24hours.com


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 Figure C.5).

Figure C.5. Successfully compiling a Java application.


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. Figure C.6 displays the output produced on my system.

Figure C.6. Running a Java application.


That's all the skills you need to create simple Java applications 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 book.

Did you Know?

Sun Microsystems 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 book 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 http://www.java24hours.com.

    Previous Section  < Day Day Up >  Next Section