This document explains how to import existing source code in 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 Java SE source code:
For more information about the different types of project templates, see Project Types Compared.
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.
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.
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
JavaGames/BlackJack/BlackJackUI
folder, and select the resources
and src
folders. The BlackJackUI project is displayed in both the Projects window and the Files window.
BlackJackLib
. 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
JavaGames/BlackJack/BlackJackLib
directory and select the src
folder. The new BlackJackLib project is displayed alongside the BlackJackUI project in both the Projects window and the Files window as illustrated below.
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.
JavaGames/Libraries/jcardshark-core.jar
, and click
Open. The JAR file is added to the project's classpath. 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.
NetBeans_projects
folder and select the BlackJackLib
project folder.
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.
BlackJack
in the Project Name field, ensure the Set
as Main Project checkbox is selected, and click Next./JavaGames/
folder, and add the BlackJackLib/src
,
BlackJackUI/src
, and BlackJackUI/resources
folders
to the project. jcardshark-core.jar
to the source folder's classpath. /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.The BlackJackUI project is displayed in both the Projects window and the Files window as illustrated below.
Note: Your project's Ant script must have a target for running the application.
For more information about using NetBeans IDE 5.5, see the following resources: