The Wayback Machine - https://web.archive.org/all/20050608012623/http://www.netbeans.org:80/kb/41/import_j2se.html

Importing Existing Java Source Code into NetBeans IDE 4.1

This document explains how to import existing source code in the NetBeans IDE projects. The first scenario outlines the steps necessary to accomplish this when working with standard Java projects, while the second describes the process when working with free-form Java projects (those using an existing Ant script). In both scenarios we'll refer to an imaginary BlackJack example project to illustrate how to set up your application code in NetBeans IDE.

There are two project templates you can use to import your existing J2SE source code:

  • Java Project with Existing Sources - Use this project template to create a standard Java project. A standard Java project uses the IDE to compile, run, and debug your application.
  • Java Project with Existing Ant Script - Use this project template to create a free-form Java project. A free-form Java project uses your own Ant script to compile, run, and debug your project.

    For more information about the different types of project templates, see Project Types Compared.

Getting Your Code Working

Our imaginary BlackJack application consists of three packages: com.jcardshark.blackjack.ui, com.jcardshark.blackjack.lib and resources. For this tutorial, we'll assume that these three packages are located in the BlackJack folder within the JavaGames folder. The screenshot below illustrates our imaginary directory tree. Notice also that the Libraries folder contains a JAR file called jcardshark-core.jar that needs to be on the classpath for the com.jcardshark.blackjack.ui and com.jcardshark.blackjack.lib packages.

A standard explorer window showing the source folder layout of the BlackJack project.


Using the IDE to Build, Run, and Debug: Standard Projects

To get started, we'll create a main project that will serve as the main entry point for our BlackJackUI application. This project will contain two source roots. Then we'll create a dependent project within which to store our application's required BlackJackLib sources. Next we'll add both the extra library and the BlackJackLib project itself to our BlackJackUI project's classpath. Finally, we'll test our work by running the application.

Creating a Project with Multiple Source Roots

  1. Choose File > New Project (Ctrl-Shift-N).
  2. Choose General > Java Project with Existing Sources. Click Next.
  3. In the Project Name field, type BlackJackUI. Notice that the IDE automatically suggests this name for the name of the project folder.

  4. Click the Browse button next to the Project Folder field and create a folder somewhere on your system called NetBeans_projects within which to house your NetBeans projects. Then select the newly-created NetBeans_projects folder, ensure the Set as Main Project box is selected, and click Next.

    Note: The path specified above should appear as follows: NetBeans_projects/BlackJackUI


  5. In the Source Packages Folder pane, click Add Folder, navigate to the JavaGames/BlackJack/BlackJackUI directory, and select the resources and src folders.

  6. (Optional) In the Test Packages Folder pane, click Browse to select the folder containing the JUnit package folders. Since our BlackJack project does not contain any JUnit tests, you can skip this step.
  7. Click Finish.

    The BlackJackUI project is displayed in both the Projects window and the Files window as illustrated below.

    The standard BlackJack project's layout as shown in the IDE's Projects window and Files window.

Creating a Dependent Project

  1. Choose File > New Project (Ctrl-Shift-N).
  2. Choose General > Java Project with Existing Sources. Click Next.
  3. In the Project Name field, type BlackJackLib.

  4. Click the Browse button next to the Project Folder field and navigate to the NetBeans_projects folder you created earlier. Ensure Set as Main Project is not selected and click Next.

    Note: The path specified above should appear as follows: NetBeans_projects/BlackJackLib


  5. In the Source Packages Folder pane, click Add Folder, navigate to the JavaGames/BlackJack/BlackJackLib directory and select the src folder.

  6. Click Finish.

    The new BlackJackLib project is displayed alongside the BlackJackUI project in both the Projects window and the Files window.

Managing the Project Classpath

At this point we need to add the additional library located in the /JavaGames/Libraries/ folder to both the BlackJackLib dependent project and the main BlackJackUI project.

  1. In the Projects window, right-click the BlackJackLib project node and choose Properties.
  2. Select the Libraries node in the Categories pane of the Properties dialog box. Then select the Compile tab in the right pane.
  3. Click Add JAR/Folder, select JavaGames/Libraries/jcardshark-core.jar, and click Open. The JAR file is added to the project's classpath.
  4. Click OK to exit the Project Properties dialog.
  5. Repeat steps 1-4 to add JavaGames/Libraries/jcardshark-core.jar to the BlackJackUI project's classpath.

Now let's add the BlackJackLib project itself to our BlackJackUI project's classpath.

  1. Again in BlackJackUI's Project Properties dialog, select the Libraries node in the Categories pane. Then select the Compile tab in the right pane.
  2. Click Add Project, navigate to the NetBeans_projects folder and select the BlackJackLib project folder.

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: Free-Form Projects

If you already have your own build.xml file, you can create a free-form project for your application. In free-form projects you have to manually link any Ant targets you need to the appropriate IDE commands and set up the project's source folders in order to build, run and debug your project. Let's take a look at using a free-form project with an existing Ant script for our BlackJack application.

Just as in the earlier example, we first need to create the project that will house our application and required sources. Next we'll map the Ant targets that will enable us to call IDE commands on our application and configure our project's classpath by adding the necessary sources and specifying the source level for our application. Finally, we'll test our application by building and running the project.

Creating a Free-Form Project

  1. Choose File > New Project (Ctrl-Shift-N).
  2. Select General > Java Project with Existing Ant Script. Click Next.
  3. On the Name and Location page of the wizard, click the Browse button next to the Location field and select NetBeans_projects. Notice that the IDE automatically suggests this directory as the location of the new project folder.

  4. Click the Browse button next to the Build Script field and select the build.xml file you want to use for the project. Note that if the Ant script you want to use is already in the directory you specified in the Location field, the IDE automatically fills in this information for you.
  5. Enter BlackJack in the Project Name field, ensure the Set as Main Project checkbox is selected, and click Next.

Linking Ant targets with IDE commands

  1. On the Build and Run Actions page of the wizard, specify which targets the IDE should run for project commands.
    Note: 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.

  2. Once you have filled in the other target fields, click Next.

Configuring Source Folders

  1. On the Source Package Folders page of the wizard, click Add Folder, navigate to the /JavaGames/ folder, and add the BlackJackLib/src, BlackJackUI/src, and BlackJackUI/resources folders to the project.

  2. In the Source Level combo box, choose the JDK version that you want to compile and run your application against. Click Next.
  3. On the Java Sources Classpath page of the wizard, select BlackJackLib in the Source Package Folder combo box. Then click Add JAR/Folder to add jcardshark-core.jar to the source folder's classpath.
  4. Select /BlackJackUI/src in the Source Package Folder combo box and then add BlackJackUI/resources, BlackJackLib/src, 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 only 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 as illustrated below.



    The free-form BlackJack project's layout as shown in the IDE's Projects window and Files window.

Running the Program

  • Choose Run > Run Main Project to run the application.
    Note: Your project's Ant script must have a target for running the application.

Next Steps

For more information about using NetBeans IDE 4.1, see the following resources:

 
 

Related Documents

Quickly create, build, and execute a simple a simple J2SE application.
A compact user's guide to the IDE, containing useful information on general IDE features for creating projects, editing files, compiling, debugging, integrating version control, and so on.
A short guide to getting your existing web applications into NetBeans IDE 4.1.
A short guide to getting your existing enterprise applications and EJB modules into NetBeans IDE 4.1.
A short guide to getting your existing J2ME platform source code into NetBeans IDE 4.1.
Transition guide for NetBeans IDE 3.6 users.
Shows you how to further configure a freeform project to fully integrate the IDE with your company's build process.
Complete list of documentation for NetBeans IDE 4.1.