The Wayback Machine - https://web.archive.org/all/20050514001149/http://www.netbeans.org:80/kb/41/quickstart.html

NetBeans IDE 4.1 Quick Start Guide

The following short tutorial takes you through some of the basic steps of developing a J2SE application in NetBeans IDE 4.1. We will create a MyLib project with a utility class, then create a MyApp project with a main class that implements a method from the library project. This document is designed to get you going as quickly as possible. For more information on working with NetBeans IDE, see the Support and Docs page on the NetBeans website.

Sample Projects

Just want to play with some projects? In the IDE, choose File > New Project, then look under the Samples folder. The IDE includes sample projects for both web and enterprise projects as well as general projects to help familiarize you with the IDE.

Setting Up a Project

First we'll create a new Java class library to contain the utility classes we'll need later. Then we need to create a new Java application to use as our main project. Once the MyApp project has been created, we'll add MyLib's classes to its classpath.

Creating a new Java class library

  1. Choose File > New Project (Ctrl-Shift-N). Under Categories, select General. Under Projects, select Java Class Library and click Next.
  2. Under Project Name, enter MyLib. Change the Project Location to any directory on your computer. From now on, we will refer to this directory as NetBeans_projects.

    Note: The path specified above should appear as follows: /NetBeans_projects/MyLib/

  3. Click Finish. The IDE creates the NetBeans_projects/MyLib folder on your system. This folder contains all of your sources and project metadata, such as the project Ant script.

    The MyLib project opens in both the Projects window and the Files window.

Creating a new Java application

  1. Choose File > New Project. Under Categories, select General. Under Projects, select Java Application and click Next.
  2. Under Project Name, enter MyApp. Make sure the Project Location is set to NetBeans_projects.
  3. Enter acrostic.Main as the main class.
  4. Ensure that the Set as Main Project and Create Main Class checkboxes are checked. Click Finish.

    The IDE creates the NetBeans_projects/MyApp folder. The MyApp project is displayed in the Project window and Main.java opens in the Source editor.

Configuring the compilation classpath

  1. Right-click the MyApp project in the Projects window and choose Properties.
  2. Select the Libraries node in the dialog's left pane. Then select the Compile tab and click Add Project.
  3. Browse to NetBeans_projects/ 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 we have not actually built the JAR file yet. This JAR file will get built when we build and run the MyApp project.
  4. Click Add Project JAR Files, then click OK to close the Project Properties dialog box.

    The JAR is added to the MyApp project's Compile-time Libraries list.

Creating and Editing Java Source Code

Now we need to create a Java package and add the method that we'll use to construct our acrostic, after which we'll implement the acrostic method in the Main class.

Creating a Java package and class file

  1. Expand the MyLib project node.
  2. Right-click the Source Packages node and choose New > Java Class. Enter the name LibClass for the new class, type org.me.mylib in the Package field, and click Finish. LibClass.java opens in the Source Editor.
  3. In LibClass.java, place the cursor on the line between the class declaration (public class LibClass {) and the constructor (public LibClass() {). Type or paste in the following method code:
  4.     public static String acrostic(String[] args) {
            StringBuffer b = new StringBuffer();
            for (int i = 0; i < args.length; i++) {
                if (args[i].length() > i) {
                    b.append(args[i].charAt(i));
                } else {
                    b.append('?');
                }
            }
            return b.toString();
        }
  5. If the code you pasted in is not formatted correctly, press Ctrl-Shift-F to reformat the entire file.
  6. Press Ctrl-S to save the file.

Editing a Java file

  1. Select the Main.java tab in the Source Editor.
  2. Delete the // TODO code application logic here comment in the main method and type the following:
    String result = Li
  3. Press Ctrl-Space to open the code completion box. The IDE offers code completion for all classes and methods in the project's compilation classpath. 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 most packages, the box displays a "Cannot find Javadoc message" until you reach a Java class.
  4. In the main method, enter a period after LibClass. The code completion box opens again. Select the acrostic(String[]args) method with its default argument args and press Enter. If available, the Javadoc viewer opens enabling you to view the popup information for the method. Next, type a semicolon after the closing parenthesis. The final line should look like this:
    String result = LibClass.acrostic(args);
  5. Press Enter to start a new line. Then type sout and press space. 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 this:
    System.out.println("Result = " + result);

Refactoring

Now that we've created our application, we'll take a look at the IDE's refactoring features which enable you to find, relocate and even automatically rename your classes.

Finding usages of a class name

  1. In the Projects window, expand MyLib > Source Packages > org.me.mylib > LibClass.java > LibClass > Methods.
  2. Right-click the acrostic method, choose Find Usages (Alt-F7) and click Next. The results are displayed in the Usages window at the bottom of the IDE.
  3. In the Usages window, double-click any result to jump to its location in the Source Editor.

Moving classes to different packages

  1. Again in the Project window, right-click the MyApp project's Source Packages node and choose New > Java Package. Name the package org.me.myapp and click Finish.
  2. Drag the Main.java node from the acrostic package and drop it into the org.me.myapp package. The Refactor Code for Moved Class dialog box asks you whether you want to change all references to the class to point to the new package.
  3. Click Next to proceed with the refactoring. The Refactoring window previews all of the references that will be changed to point to the new package. Double-click any reference to jump to its location in the Source Editor. You can deselect the check box next to any reference to exclude it from the refactoring.
  4. Click Do Refactoring in the lower left of the Refactoring window.

    The IDE updates the package statement in Main.java.

Renaming classes

  1. Select the Main.java tab in the Source Editor.
  2. Right-click the word Main in the class declaration on or around line 15 and choose Refactor > Rename.
  3. In the New Name field, write AcrosticExample. Then click Next.
  4. Click the Do Refactoring button.

    The IDE renames the class and all references to it.

Undoing refactoring

  1. Choose Refactor > Undo [Rename].
  2. Choose Refactor > Redo [Rename].

Compiling and Running a Project

Now we need to set the main class and execution arguments so we can run our project. We'll also take a look at the IDE's cleaning, building, and Javadoc generation features.

Setting the main class and execution arguments

  1. Right-click the MyApp project node, choose Properties, and select the Run node in the dialog's left pane. Notice that acrostic.Main appears in the Main Class field. Since we have renamed the main class and put it in a different package, we have to enter the new class name as the main class.
  2. Click the Browse button and select org.me.myapp.AcrosticExample. Then click Select Main Class.
  3. Enter However we all feel zealous in the Arguments field and click OK.

Running the main project

  1. Choose Run > Run Main Project (F6) from the Run menu. Double-click the Output window to maximize it so you can see all the output. Note that Ant builds MyLib.jar first and then compiles MyApp using it. Finally, it prints the output from the program, Result = Hello (the acrostic of the phrase that was passed to the program as an argument).
  2. Select the Files window and expand the MyApp project folder. The built class files are in the build folder.
  3. Press F6 to run the program again. Nothing new needs to be compiled, but the program is run.

Cleaning and building a project

  1. Choose Build > Clean and Build Main Project (Shift-F11). Both the MyLib project and the MyApp project are cleaned and rebuilt as part of the process.
  2. Right-click the MyLib project node in the Projects window and choose Clean Project. Just the MyLib project is cleaned.

Building an individual project

  1. Right-click the MyLib project node in the Projects window.
  2. Choose Build Project from the contextual menu.

Generating Javadoc

  1. Select the MyLib project.
  2. Choose Build > Generate Javadoc for Project "MyLib" from the IDE's main menu.

    The IDE displays Javadoc output in the Output window, and your web browser opens displaying the Javadoc.

Testing and Debugging a Project

Now we'll create and run a test for our project using JUnit and then run it in the IDE's debugger to check for errors.

Creating JUnit tests

  1. Right-click the LibClass.java node in the Projects window and choose Tools > JUnit Tests > Create Tests (Ctrl-Alt-J). Click OK to run the command with the default options. The IDE creates the org.me.mylib package under Test Packages and the LibClassTest.java file.
  2. With the LibClass.java tab selected in the Source Editor, press Ctrl-Alt-K to open the test class. Delete the body of the testAcrostic method and type or paste in the following:
    System.err.println("Running testAcrostic...");
    String result = LibClass.acrostic(new String[]
                      {"fnord", "polly", "tropism"});
    assertEquals("Correct value", "foo", result);
  3. Save the file by pressing Ctrl-S.

Running JUnit tests

  1. Select the MyLib project node and choose Run > Test "MyLib" (Alt-F6). The MyLib (test) tab opens in the Output window. The JUnit test cases are compiled and run. LibClassTest.testAcrostic 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 > Run File  > Test "LibClass.java" from the Run menu.

Debugging a project

  1. Select the AcrosticExample.java tab in the Source Editor, place the caret in LibClass, and press Alt-G. LibClass.java opens in the Source Editor.
  2. Go to the acrostic method and place the caret anywhere inside b.append(args[i].charAt(i));. Then press Ctrl-F8 to set a breakpoint.
  3. Choose Run > Debug Main Project (F5). The IDE opens the Debugger windows and runs the project in the debugger until the breakpoint is reached.
  4. Select the Local Variables window and expand the args node. The array of strings contains the phrase you entered as the command arguments.
  5. Click Continue (Ctrl-F5) in the toolbar to step through the program and watch the acrostic being constructed.

    When the program reaches the end, the debugger windows close.

Customizing the Build Process

In this section we'll use the IDE to edit Ant properties and targets.

Overriding an Ant property

  1. In the Projects window, right-click the project's node and choose Clean Project.
  2. In the Files window, expand the MyApp project node and the nbproject folder.
  3. Double-click project.properties to view all of the Ant properties generated by the IDE for the project.
  4. Copy the line containing build.dir=build. This property sets the output directory for compiled classes.
  5. In the Files window, expand the private folder and double-click private.properties.
  6. Paste the build.dir=build property into the file and change the property to read build.dir=build/production
  7. Save the file (Ctrl-S).
  8. Choose Build > Build Main Project (F11).

    The compiled classes are built to the build/production folder.

Adding to an Ant target

  1. In the Files window, go to the nbproject folder for the MyApp project.
  2. Double-click build-impl.xml to open it in the Source Editor. This file contains all of the Ant targets generated by the IDE. Each target has a -pre target and a -post target that you can use to add processing instructions that the IDE runs before or after running the target. Do not change the targets in this file — this file is generated automatically by the IDE.
  3. In the Files window, go to the MyApp project folder and double-click build.xml. This file is where you override targets from build-impl.xml.
  4. Type or paste the following Ant target to run RMIC on all classes whose name starts with Remote:
    <target name="-post-compile">
    <rmic base="${build.classes.dir}" includes="**/Remote*.class"/>
    </target>

Next Steps

For more information about using NetBeans IDE 4.1, see the following resources:

 
 

Related Documents

A short guide to getting your existing Java applications into NetBeans IDE 4.1.
A compact user's guide to the IDE, containing useful information on general IDE features for creating projects, editing files, compiling, debugging, integrating version control, and so on.
Create and deploy a simple web application.
Create, expose, and consume a simple web service.
Generate a set of CMP entity beans from a database and access the beans from a Web application client.
Quickly create, build, and execute a simple J2ME MIDP application.
Complete list of documentation for NetBeans IDE 4.1.