FeaturesPluginsDocs & SupportCommunityPartners

Importing Existing Java Source Code into NetBeans IDE 4.0

This document explains how to import existing source code in the NetBeans IDE projects. There are two project templates you can use to import your existing J2SE source code:


Getting Your Code Working

To illustrate how to set up your application code in NetBeans IDE, we'll use the BlackJack example project below. The BlackJack application consists of two packages: com.jcardshark.blackjack.ui and com.jcardshark.blackjack.lib. The source folder layout is shown below. The Libraries folder contains a JAR file called jcardshark-core.jar that needs to be on the classpath for both packages.


Using the IDE to Build, Run, and Debug


Creating a project

  1. Choose File > New Project (Ctrl-Shift-N).
  2. Choose General > Java Project with Existing Sources. Click Next.
  3. In Source Packages Folder, click Browse to select BlackJackUI folder in the JavaGames/BlackJack folder.
  4. (Optional) In Test Packages Folder, click Browse to select the folder containing the JUnit package folders. Since the BlackJack project does not contain any JUnit tests, you can skip this step.
  5. In Project Name, type BlackJackUI. The name is also used as the name of the project folder.
  6. Somewhere on your system, create a folder called NetBeans_projects to house your NetBeans project folders. In Project Location, click Browse to select the NetBeans_projects folder.
  7. Leave the Set as Main Project box selected.
  8. Click Finish. The BlackJackUI project is displayed in both the Projects window and the Files window.

  9. Repeat steps 1-8 to create a project for the BlackJackLib source folder. Do not set the BlackJackLib project as the main project.

Setting the project classpath

  1. In the Projects window, right-click the BlackJackUI project node and choose Properties. Select Compiling Sources in the left panel of the dialog box.
  2. Click Add JAR/Folder, select JavaGames/Libraries/jcardshark-core.jar, and click OK. The JAR file is added to the project's classpath.
  3. In the Compiling Sources panel of the Project Properties dialog box, click Add Project. Navigate to the NetBeans_projects folder and select the BlackJackLib project folder.
  4. Repeat steps 1-2 to add JavaGames/Libraries/jcardshark-core.jar to the BlackJackLib project's classpath.

Running the application

  1. Choose Run > Run Main Project to run the application.
  2. In the dialog that appears, set BlackJack.java as the main class.

Using Your Own Ant Script to Compile, Run, and Debug


Creating a project

  1. Choose File > New Project (Ctrl-Shift-N).
  2. Select General > J2SE Project with Existing Ant Script. Click Next.
  3. In Location, click Browse and select E:/JavaGames/BlackJack. The IDE fills in the rest of the fields automatically. By default, the IDE suggests to put your NetBeans project folder in the same folder specified in Location.

Linking Ant targets with IDE commands

  • Specify which targets the IDE should run for project commands. Leave Generate Javadoc and Test Project empty, since the Ant script does not contain targets for these commands. These commands will not be available in the IDE until you write targets for them. Click Next

Configuring source folders

  1. Click Add Folder and add both of the BlackJack source package folders to the project.
  2. In Source Level, choose the JDK that you want to compile and run your application against. Click Next.
    Note: If the target JDK is different than the IDE's default JDK, you have to register the JDK by choosing Tools > Java Project Manager.
  3. Select BlackJackLib and click Add JAR/Folder to add jcardshark-core.jar to the source folder's classpath.
  4. Select BlackJackUI and add both BlackJackLib and jcardshark-core.jar to the source folder's classpath.
    Note: This classpath is not used for compilation or execution - your Ant script handles the classpath for these tasks. These settings tell the IDE which classes to include in code completion and refactoring.
  5. Click Finish. The BlackJackUI project is displayed in both the Projects window and the Files window.

Running the program

  • Choose Run > Run Main Project to run the application.

Bookmark this page

del.icio.us furl simpy slashdot technorati digg
Companion
Projects:
MySQL Database Server   Open JDK: an Open SourceJDK   GlassFish Community: an Open Source Application Server    Mobile & Embedded Community    Open Solaris   java.net - The Source for Java Technology Collaboration   Open ESB - The Open Enterprise Service Bus Powered by