Senin, 28 Januari 2013

Developing General Java Applications

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.

Creating a Java Class Library Project

  1. Choose File > New Project (Ctrl-Shift-N). Under Categories, select Java. Under Projects, select Java Class Library. Click Next.
  2. 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 NetBeansProjects.
    Note: The path specified above should appear as follows in the Project Folder field of the wizard: /NetBeansProjects/MyLib/
  3. (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.
  4. Click Finish. The MyLib project opens in both the Projects window and the Files window.

Creating a Java Application Project

  1. Choose File > New Project. Under Categories, select Java. Under Projects, select Java Application. Click Next.
  2. Under Project Name, type MyApp. Make sure the Project Location is set to NetBeansProjects.
  3. (Optional) Check the Use Dedicated Folder for Storing Libraries checkbox.
  4. Enter acrostic.Main as the main class.
  5. Ensure that the Create Main Class checkbox is checked.
  6. 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:
  1. In the Projects window, right-click the Libraries node for the MyApp project and choose Add Project as shown in the image below. Image showing the Add Project command that is offered when you right-click a project's node.
  2. Browse to NetBeansProjects/ 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.
  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.

Tidak ada komentar:

Posting Komentar