Introduction to 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 6.0. This tutorial assumes you already have some familiarity with developing Java applications. Along the way, we will demonstrate some of the IDE features that simplify application development.

We will be developing an application that converts several words into a single word that contains one letter from the other words. The resulting word is composed of the first letter of the first word, the second letter of the second word, the third letter of the third word, and so on. The resulting word is called an acrostic.

Contents

Content on this page applies to NetBeans IDE 6.0

Project Setup

The application we create will contain two projects:

Note: Strictly speaking, two projects are not needed for such a simple application. We are merely using two projects to demonstrate features that you might need for a more complex application.

First we will create a new Java class library to contain the utility classes we will 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 Java. 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 in the Project Folder field of the wizard: /NetBeans_projects/MyLib/

  3. Click Finish. 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 Java. 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.
  5. 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, we have to add MyLib to the classpath of MyApp. Doing so will also ensure that classes in the MyApp project can refer to classes in the MyLib project without causing compilation errors. In addition, this will enable you to use code completion in the MyApp project to fill in code based on the MyLib project.

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.
  2. 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.
  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 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. 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) {
            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 Alt-Shift-F to reformat the entire file.
  6. Press Ctrl-S to save the file.

Editing a Java File

Now we will add some code to Main.java. In doing so, we will demonstrate 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 immediate after Li. In the next step we will show how to 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 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 most packages, the box displays a "Cannot find Javadoc message" until you reach a Java class.

  7. In the main method, enter 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 shows the highlights the args parameter.
  9. Press Enter to accept args as the parameter.
  10. Type a semicolon.

    The final line should look like this:

    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 this:
    System.out.println("Result = " + result);
  12. Press Ctrl-S to save the file.

Compiling and Running the 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 the main class is already set to acrostic.Main.
  2. 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.
  2. 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).
  3. Select the Files window and expand the MyApp project folder. The built class files are in the build folder.
  4. Press F6 to run the program again. Nothing new needs to be compiled, but the program is run.

Cleaning and Building the Project

If you build a project and then later change classes, it is good to remove all build artifacts from your system and create a fresh build of the application. You can do this with the Clean and Build command.

  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 "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 the 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. In the JUnit test, we test the LibClass by passing a phrase to the acrostic method and using an assertion to indicate what we 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 acrostic() method.
  4. In place of the deleted lines, 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);
  5. 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. 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 > Run File  > Test "LibClass.java" from the Run menu.

The JUnit API documentation is available from the IDE. Choose Help > Javadoc References > JUnit API.

You can learn more about JUnit by visiting http://www.junit.org

Debugging the Project

  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 Run > 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 and expand the args node. The array of strings contains the phrase you entered as the command arguments.
  4. Click Step Into (F7) in the toolbar to step through the program and watch the acrostic being constructed.

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

Send Us Your Feedback