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:
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.

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
- Choose File > New Project (Ctrl-Shift-N).
- Choose General > Java Project with Existing Sources. Click Next.
- In the Project Name field, type BlackJackUI. Notice that the IDE automatically
suggests this name for the name of the project folder.
- 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
- In the Source Packages Folder pane, click Add Folder, navigate to the
JavaGames/BlackJack/BlackJackUI
directory, and select the resources and src folders.
- (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.
- Click Finish.
The BlackJackUI project is displayed in both the Projects
window and the Files window as illustrated below.
Creating a Dependent Project
- Choose File > New Project (Ctrl-Shift-N).
- Choose General > Java Project with Existing Sources. Click Next.
- In the Project Name field, type
BlackJackLib.
- 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
- In the Source Packages Folder pane, click Add Folder, navigate to the
JavaGames/BlackJack/BlackJackLib directory and select the src
folder.
- 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.
- In the Projects window, right-click the BlackJackLib project
node and choose Properties.
- Select the Libraries node in the Categories pane of the Properties dialog
box. Then select the Compile tab in the right pane.
- Click Add JAR/Folder, select
JavaGames/Libraries/jcardshark-core.jar,
and click Open. The JAR file is added to the project's classpath.
- Click OK to exit the Project Properties dialog.
- 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.
- Again in BlackJackUI's Project Properties dialog, select the
Libraries node in the Categories pane.
Then select the Compile tab in the right pane.
- Click Add Project, navigate to the
NetBeans_projects folder
and select the BlackJackLib project folder.
Running the Application
- Choose Run > Run Main Project to run the application.
- 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
- Choose File > New Project (Ctrl-Shift-N).
- Select General > Java Project with Existing Ant Script. Click Next.
- 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.
- 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.
- 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
- 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.
- Once you have filled in the other target fields, click Next.
Configuring Source Folders
- 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.
- In the Source Level combo box, choose the JDK version that you want to
compile and run your application against. Click Next.
- 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.
- 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.
- Click Finish.
The BlackJackUI project is displayed in both the Projects
window and the Files window as illustrated below.
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: