Developing and Deploying General Java Applications
The following short tutorial takes you through some of the basic steps of developing a Java SE application in NetBeans IDE. This tutorial assumes you already have some familiarity with developing Java applications. Along the way, you will see some of the IDE features that simplify application development.
You will create an application that converts several words into a single word that contains one letter from each of the other words. The resulting word is called an acrostic.
This tutorial takes approximately 30 minutes to complete. If you would like to do a quicker "Hello World" tutorial, see the NetBeans IDE Java Quick Start Tutorial.
Contents
To complete this tutorial, you need the software and resources listeds in the following table.
Project Setup
The application you create will contain two projects:
- A Java Class Library project in which you will create a utility class.
- A Java Application project with a main class that implements a method from the library project's utility class.
After you create the projects, you will add the library project to the classpath of the application project. Then you will code the application. The library project will contain a utility class with an acrostic
method. The acrostic
method takes an array of words as a parameter and then generates an acrostic based on those words. The MyApp project will contain a main class that calls the acrostic
method and passes the words that are entered as arguments when the application is run.
Note: Strictly speaking, two projects are not needed for such a simple application. This tutorial uses two projects to demonstrate features that you might need for a more complex application.
- Choose File > New Project (Ctrl-Shift-N). Under Categories, select Java. Under Projects, select Java Class Library. Click Next.
- Under Project Name, type
MyLib
. Change the Project Location to any directory on your computer. From now on, this tutorial refers 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/
- (Optional) Select the Use Dedicated Folder for Storing Libraries checkbox and specify the location for the libraries folder. See Sharing Project Libraries for more information on this option.
- Click Finish. The MyLib project opens in both the Projects window and the Files window.
- Choose File > New Project. Under Categories, select Java. Under Projects, select Java Application. Click Next.
- Under Project Name, type
MyApp
. Make sure the Project Location is set to NetBeans_projects
.
- (Optional) Check the Use Dedicated Folder for Storing Libraries checkbox.
- Enter
acrostic.Main
as the main class.
- Ensure that the Set as Main Project and Create Main Class checkboxes are checked.
- Click Finish. The MyApp project is displayed in the Project window and
Main.java
opens in the Source Editor.
Since MyApp is going to depend on a class in MyLib, you have to add MyLib to the classpath of MyApp. Doing so also ensures that classes in the MyApp project can refer to classes in the MyLib project without causing compilation errors. In addition, this enables you to use code completion in the MyApp project to fill in code based on the MyLib project. In the IDE, the classpath is visually represented by the Libraries node.
To add the library's utility classes to the project classpath:
- In the Projects window, right-click the Libraries node for the MyApp project and choose Add Project as shown in the image below.
- 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 you have not actually built the JAR file yet. This JAR file will get built when you build and run the MyApp project.
- Click Add Project JAR Files.
- 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 you need to create a Java package and add the method that you will use to construct the acrostic. After that you need to implement the acrostic
method in the Main
class.
Creating a Java Package and Class File
- 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.
- In
LibClass.java
, place the cursor on the line after the class declaration (public class LibClass {
.
- Type or paste in the following method code:
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();
}
- If the code that you pasted in is not formatted correctly, press Alt-Shift-F to reformat the entire file.
- Press Ctrl-S to save the file.
Editing a Java File
Now you will add some code to Main.java
. In doing so, you will see the Source Editor's code completion and code template (abbreviation) features.
- 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
.
- Delete the
// TODO code application logic here
comment in the main
method.
- In place of the comment type the following:
String result = Li
Leave the cursor immediately after Li
. In the next step you will use code completion to turn Li
into LibClass
.
- 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 be there.
- Press Ctrl-Space again to display a longer list of possible matches.
LibClass
should be in this list.
- 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 this package, the box displays a "Cannot find Javadoc" message.
- In the main method, type a period (.) after
LibClass
. The code completion box opens again.
- Select the
acrostic(String[]args)
method and press Enter. The IDE fills in the acrostic
method and the highlights the args
parameter.
- Press Enter to accept
args
as the parameter.
- Type a semicolon (;).
The final line should look like the following line.
String result = LibClass.acrostic(args);
- 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 the following line.
System.out.println("Result = " + result);
- Press Ctrl-S to save the file.
Note: sout
is one of many code templates that are available in the Source Editor. You can find a list of code templates in the keyboard shortcuts card, which you can open by choosing Help > Keyboard Shortcuts Card.
Compiling and Running the Application
Now you need to set the main class and execution arguments so that you can run the project.
Note: By default, the projects have been created with the Compile on Save feature enabled, so you do not need to compile your code first in order to run the application in the IDE. For more information on the Compile on Save feature, see the Compile on Save section of the Creating, Importing, and Configuring Java Projects guide.
Setting the Main Class and Execution Arguments
The output of this program is based on arguments that you provide when you run the program. As arguments, you will provide five words, from which the acrostic "Hello" will be generated. The acrostic is assembled from the first letter of the first word, the second letter of the second word, the third letter of the third word, and so on.
To add the arguments for the IDE to use when running the application:
- Right-click the MyApp project node, choose Properties, and select the Run node in the dialog's left pane.
The main class should already be set to acrostic.Main
.
- Type
However we all feel zealous
in the Arguments field and click OK.
Running the Application
Now that you have created the application and provided runtime arguments for the application, you can test run the application in the IDE.
To run the application in the IDE:
Testing and Debugging the Application
Now you will create and run a test for the project using JUnit and then run the application in the IDE's debugger to check for errors. In the JUnit test, you will test the LibClass by passing a phrase to the acrostic
method and using an assertion to indicate what you think the result should be.
Creating JUnit Tests
- 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.
- 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.
- In
LibClassTest.java
, delete the body of the public void testAcrostic()
method.
- 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);
- Save the file by pressing Ctrl-S.
Running JUnit Tests
- Select the MyLib project node and choose Run > Test Project (MyLib) or press 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.
- 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 > Test File.
The JUnit API documentation is available from the IDE. Choose Help > Javadoc References > JUnit VersionNumber
.
You can learn more about JUnit by visiting http://www.junit.org
Debugging the Application
In this section, you will use the debugger to step through the application and watch the values of variables change as the acrostic is assembled.
To run the application in the debugger:
- 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.
- Choose Debug > Debug Main Project (Ctrl-F5). The IDE opens the Debugger windows and runs the project in the debugger until the breakpoint is reached.
- Select the Local Variables window in the bottom of the IDE and expand the
args
node. The array of strings contains the phrase you entered as the command arguments.
- Press F7 (or choose Debug > Step Into) to step through the program and watch the
b
variable change as the acrostic is constructed.
When the program reaches the end, the debugger windows close.
For more information, see Writing JUnit Tests in NetBeans IDE.
Building and Deploying the Application
Once you are satisfied that your application works properly, you can prepare the application for deployment outside of the IDE. In this section you will build the application's JAR file and then run the JAR file from the command line.
Building the Application
The main build command in the IDE is the Clean and Build command. The Clean and Build command deletes previously compiled classes and other build artifacts and then rebuilds the entire project from scratch.
Notes: There is also a Build command, which does not delete old build artifacts, but this command is disabled by default. See the Compile on Save section of the Creating, Importing, and Configuring Java Projects guide for more information.
To build the application:
- Choose Run > Clean and Build Main Project (Shift-F11).
Output from the Ant build script appears in the Output window. If the Output window does not appear, you can open it manually by choosing Window > Output > Output.
When you clean and build your project, the following things occur:
- Output folders that have been generated by previous build actions are deleted ("cleaned"). (In most cases, these are the
build
and dist
folders.)
build
and dist
folders are added to your project folder (hereafter referred to as the PROJECT_HOME folder).
- All of the sources are compiled into
.class
files, which are placed into the PROJECT_HOME/build
folder.
- A JAR file containing your project is created inside the
PROJECT_HOME/dist
folder.
- If you have specified any libraries for the project (in addition to the JDK), a
lib
folder is created in the dist
folder. The libraries are copied into dist/lib
.
- The manifest file in the JAR is updated to include entries that designate the main class and any libraries that are on the project's classpath.
Note: You can view the contents of the manifest in the IDE's Files window. After you have built your project, switch to the Files window and navigate to dist/MyApp.jar
. Expand the node for the JAR file, expand the META-INF
folder, and double-click MANIFEST.MF
to display the manifest in the Source Editor.
Main-Class: acrostic.Main
Class-Path: lib/MyLib.jar
(To find more about manifest files, you can read this chapter from the Java Tutorial.)
Running the Application Outside of the IDE
To run the application outside of the IDE:
- On your system, open up a command prompt or terminal window.
- In the command prompt, change directories to the
MyApp/dist
directory.
- At the command line, type the following statement:
java -jar MyApp.jar However we all feel zealous
The application then executes and returns the following output as shown in the image below:
Result = Hello
Distributing the Application to Other Users
Now that you have verified that the application works outside of the IDE, you are ready to distribute the application.
To distribute the application:
- On your system, create a zip file that contains the application JAR file (
MyApp.jar
) and the accompanying lib
folder that contains MyLib.jar
.
- Send the file to the people who will use the application. Instruct them to unpack the zip file, making sure that the
MyApp.jar
file and the lib
folder are in the same folder.
- Instruct the users to follow the steps in the Running the Application Outside of the IDE section above.
Other Common Tasks
You have now completed the main part of the tutorial, but there are still some basic tasks that have not been covered. This section includes a few of those tasks.
Making the Javadoc Available in the IDE
To view the JavaSE API documentation in the NetBeans IDE, use the Source > Show Documentation command or choose Window > Other > Javadoc from the main menu to view API documentation in a separate window.
However, for some third-party libraries, API documentation is not available. In these cases, the Javadoc resources must be manually associated with the IDE..
To make the Javadoc API documentation available for the Show Javadoc command:
- Download the Javadoc API documentation source.
- Choose Tools > Libraries.
- In the Libraries list, select the library that your project is using.
- Click the Javadoc tab.
- Click the Add ZIP/Folder button and navigate to the zip file or the folder that contains the Javadoc API documentation on your system. Select the zip file or the folder and click the Add ZIP/Folder button.
- Click Close.
Generating Javadoc for a Project
You can generate compiled Javadoc documentation for your project based on Javadoc comments that you have added to your classes.
To generate Javadoc documentation for a project:
- Select the MyLib project.
- Choose Run > Generate Javadoc for "MyLib" from the IDE's main menu.
The generated Javadoc is added to the dist
folder of the project. In addition, the IDE opens a web browser that displays the Javadoc.
Next Steps
For more information about using NetBeans IDE to develop Java SE applications, see the following resources: