
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:
- Java Project with Existing Source - 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.
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
- Choose File > New Project (Ctrl-Shift-N).
- Choose General > Java Project with Existing Sources. Click Next.
- In Source Packages Folder, click Browse to select
BlackJackUI
folder in the JavaGames/BlackJack folder.
- (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.
- In Project Name, type BlackJackUI. The name is also used as the name
of the project folder.
- 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.
- Leave the Set as Main Project box selected.
- Click Finish. The BlackJackUI project is displayed in both the Projects
window and the Files window.
- 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
- In the Projects window, right-click the BlackJackUI project node and
choose Properties. Select Compiling Sources in the left panel of the
dialog box.
- Click Add JAR/Folder, select
JavaGames/Libraries/jcardshark-core.jar,
and click OK. The JAR file is added to the project's classpath.
- 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.
- Repeat steps 1-2 to add
JavaGames/Libraries/jcardshark-core.jar
to the BlackJackLib project's classpath.
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
Creating a project
- Choose File > New Project (Ctrl-Shift-N).
- Select General > J2SE Project with Existing Ant Script. Click Next.
- 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
- Click Add Folder and add both of the BlackJack source package folders
to the project.
- 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.
- Select BlackJackLib and click Add JAR/Folder to add
jcardshark-core.jar
to the source folder's classpath.
- 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.
- 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.
|
Related Documents
Quickly create, build, and execute a simple a simple
J2SE application.
Quickly create, build, and deploy a simple HelloWeb
web application.
A short guide to getting your existing web applications
into NetBeans IDE 4.0.
Transition guide for NetBeans IDE 3.6 users.
A complete list of docs for NetBeans IDE 4.0.
|