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.
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.
Creating a Java Class Library Project
- 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 asNetBeansProjects
.Note: The path specified above should appear as follows in the Project Folder field of the wizard:/
NetBeansProjects
/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.
Creating a Java Application Project
- 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 toNetBeansProjects
. - (Optional) Check the Use Dedicated Folder for Storing Libraries checkbox.
- Enter
acrostic.Main
as the main class. - Ensure that the Create Main Class checkbox is checked.
- 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, 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
NetBeansProjects/
and select theMyLib
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.
Tidak ada komentar:
Posting Komentar