Part 1 - Creating a New Project in Eclipse

https://eclipse-tutorial.dev.java.net/eclipse-tutorial/part1.html

Developing open source Java applications with java.net and Eclipse

Part 1 - Creating a new project in Eclipse

Martin Pllu
martinpllu-AT-dev.java.net

Introduction


This series of tutorials show how Eclipse can be used to develop open source Java applications hosted on java.net. The target audience is developers who have a working knowledge of Java, but are new to Eclipse.
Today we'll see how to create a new project in Eclipse. Even if you don't need to do this (for example, if you plan to contribute to an existing project), I'd recommend that you follow along. The tutorial introduces some important Eclipse concepts.
In Part 2 we'll cover Team development with Eclipse and CVS.
Before we start the tutorial, let's look at some of the key features of Eclipse and java.net.

Key features of Eclipse


Eclipse provides a powerful and feature rich integrated development environment (IDE) for Java. To get an idea of what it can do, skim through the links below (from the online Eclipse help).

·  Java Development Tooling Tips and Tricks

·  Base Eclipse Platform Tips and Tricks

If you haven't used an IDE before, or last used one a few years ago, please check Eclipse out. You'll be amazed by its ease of use and advanced development features. I switched to Eclipse after years of developing in Emacs, and it's made me much more productive. It's worth using for the automated refactoring support alone!
Eclipse runs on Windows, Linux, Mac OSX, Solaris, AIX and HP-UX. It's written in Java, but uses a graphical toolkit called SWT. This toolkit is a Java wrapper for native platform widgets, and it produces a very slick look and feel.
Eclipse is actually a generic application platform with a sophisticated plugin architecture - the Java IDE is just one set of plugins. You can develop your own plugins, or use Eclipse as a basis for "rich client" applications, e.g. a web browser or a money manager. See the Platform Plug-in Developer Guide for more.
Eclipse is free open source software developed by the independent Eclipse Foundation, which is backed by industry heavy hitters such as IBM Rational, Borland, Red Hat, and SuSE. Its open source pedigree has made it ideal for open source development. For example, its built-in CVS client GUI (covered in Part 2) makes it a snap to work with CVS hosting sites like java.net.
The Eclipse documentation is excellent - see the main website and the online help.
There's an active community of third party Eclipse plugin developers, both open source and commercial. Good plugin directories are available here and here. As an Eclipse user, you're regularly rewarded with great new features from official Eclipse releases and from the plugin development community.

Key features of java.net


java.net is a thriving online Java community featuring news, articles, forums, wikis, and hosting for many open source projects. It was founded in 2003 by Sun Microsystems to help encourage open source Java development, however (like the Eclipse Foundation) java.net is completely independent. A number of communities have been formed on java.net to cater for different technologies and interests, for example Java Tools, Java Enterprise, Jini, and Java Linux. Some communities such as Javapedia and Global Education and Learning are great places to learn about Java and to share knowledge. It's the communites that really set java.net apart from other development hosting websites (like sourceforge).
It's easy to set up a new project - all you need is a good idea and the Project Request Forms. You can browse the online project directory which includes both projects hosted at java.net and linked projects (which are hosted elsewhere but affiliated with java.net). Before starting a new project, please look at the existing projects to make sure that you're not reinventing the wheel. If your idea can be used to enhance an existing project, the end result is often better than several similar, but incompatible, projects. If you want to contribute to an existing project, speak to the owners listed on the project's website.
java.net uses CVS for version control of source code. If you haven't used CVS before, don't worry, Part 2 will give you a step-by-step guide. There's a Web interface for viewing the contents of files checked into CVS, for example see here. This is handy for linking to code or documentation in CVS.
A number of useful tools are provided to help you work as part of a team, for example issue trackers, announcement boards, discussion forums, and mailing lists. Project owners can easily administer project memberships and roles. As a project member, you can choose to be notified by email whenever a project change is made, for example CVS checkins, new discussion forum entries, etc. RSS feeds are also available so that anyone can keep an eye on your project. Project webpages are easy to set up - you just check HTML into CVS. A good search engine is also provided for project-specific or site-wide searches.

Tutorial Part 1 - Setting up a new project in Eclipse
In today's tutorial, we'll be working with Eclipse to create and configure a new Java project. We won't be working with java.net until the next instalment.

Prerequisites

·  Download Eclipse 3.0 or later from here.

·  Java 1.3 or later is required. If you don't already have a suitable Java installation, download one from http://java.sun.com.

·  If you have problems downloading or starting Eclipse, see the Eclipse FAQ.

Starting Eclipse
For the purposes of the tutorial, I'm going to set up a project called javatools-demo.
First start up Eclipse and supply a path to a new folder which will serve as your workspace. The workspace is a folder which Eclipse uses to store your source code.
https eclipse tutorial dev java net eclipse tutorial part1 images pic035 jpg
Avoid checking the "Use this as the default..." box, which will always start up Eclipse with the same workspace. You'll probably find it useful to be able to switch between different workspaces on startup.
When Eclipse starts, you'll see the Welcome page:
https eclipse tutorial dev java net eclipse tutorial part1 images newimg030 jpg

Getting help


Note that you can access Eclipse help at any time via Help->Help Contents. This is the same as the help on the eclipse.org website.

Creating a project


Close the Welcome page. Right-click in the Navigator panel, and select New->Project:

Select "Java Project" and then "Next". Enter the name of your project:
https eclipse tutorial dev java net eclipse tutorial part1 images pic017 jpg
Click "Finish". If you get prompted about switching to the Java perspective, select "Yes".

Perspectives
Eclipse provides a number of perspectives for different types of development task. You can see all of the available perspectives by selecting Window->Open Perspective->Other...

The "Java" perspective (which we're currently in) provides editors, views, and menu items which are useful for developing Java applications. The "Resource" perspective (which we started off from) provides a more basic set of tools, for example a Windows Explorer style file navigator.
You can quickly switch between perspectives using the buttons to the top right of the Eclipse window:


Note that projects are simply directories stored under the directory that you selected as your workspace. Look on the file system and see for yourself.

Use an alternative JRE


In the Package Explorer panel of the Java perspective, right-click on your newly created project and select "Properties". The Properties dialog opens on Java Build Path, which controls the project's classpath.
https eclipse tutorial dev java net eclipse tutorial part1 images pic013 jpg
For now, the only entry is the JRE System Library shown in the "Libraries" tab. This defaults to the same JRE that was used to run Eclipse. In my case, a Java 5.0 installation was used. Depending on what versions of Java you have installed, you may see a different JRE here.
You might want to use a different JRE, for example the default may be Java 5.0 but you want your project to be compatible with Java 1.4 or 1.3 (your application will be useful to more people if it's backwards compatible with older JREs).

If you're happy with the default JRE, skip to the next section.


Ensure that you have the appropriate JDK or JRE version installed on your machine. If not, download and install from http://java.sun.com.
First click on "Add Library":
https eclipse tutorial dev java net eclipse tutorial part1 images pic018 jpg
Click "Next" and then "Installed JREs..."
https eclipse tutorial dev java net eclipse tutorial part1 images pic019 jpg
Click "Add".
In the next dialog, browse to the JRE or JDK that you want to use, and enter a name:

Click "OK" and then choose the new JRE:

Click "OK" and then "Finish". Your project will now be compiled against the new JRE.

Create source folders


Switch to the "Source" tab in the Properties dialog.

Eclipse projects can contain one or more source folders which contain Java source files. By default, the project directory itself is the only source folder.
Using multiple source folders is a good way to separate different types of code. This can make your project easier to work with and simplifies the Ant build scripts which we'll write later. We're going to set up separate source folders for application code and unit test code.
Click on "Add Folder" and type "src":

Click "Yes" when prompted.

Click on "Add Folder..." again and then "Create New Folder...". This time, call the folder "test". Click "OK" twice.
You'll see that we now have two source folders:

Clicking on "Order and Export" shows you the complete classpath used for compiling any Java source files in the project (we haven't created any source code yet!).
The classpath currently contains the two source folders plus the JRE:
.

Output folder
When you use custom source folders, the compiler stores the generated class files in a separate folder called the "output folder". As you can see from the dialog above, the output folder is set to a "bin" subdirectory under the project. Leave this setting alone unless you have a good reason.
Note that you won't see the bin subdirectory or class files in the Package Navigator by default. Switch to the Resource perspective to see an unfiltered view of all project files and directories.

Import third party JARs


If your application has a dependency on a third party library, you can import it into your project as a JAR file and add it to your project's classpath.
Say that the demo application needs to process dates, and we want to use the excellent Joda Time API.
First download a binary distribution of the library and extract it somewhere on your file system. Then create a new folder in your project as shown below:

When prompted, call the folder "lib". Right click on your new folder and select "Import..."

In the resulting dialog, select "File System" and then "Next". Browse to the location that you extracted the library to and select the JAR file.
Note that when you click "Browse" you must navigate to the directory containing the JAR file, otherwise you'll end up importing some unwanted folders as well as your JAR file.

Click "Finish". You should end up with something like:

Now to add the JAR to the project classpath. Right-click on the project and select "Properties". Go to the "Libraries" tab in "Java Build Path":

Click on "Add JARs..." and navigate to the freshly imported JAR. Click "OK"

Import source code

If you don't have any existing source code to import into Eclipse, skip to the next section.


Import code by right clicking on your project selecting "Import".

If your code is stored in a JAR or ZIP file, choose "Zip file". Otherwise, choose "File System". The rest of the process is pretty self explanatory.

Create a class


If you want to create some new Java code, right-click on the "src" source folder and select "New->Class"

First enter the package that your class belongs to.

Although reversing the domain name is common practise for package names, please DON'T use net.java as a project prefix. See this wiki entry for an explanation..


I've gone for org.jtdemo.
The rest of the dialog is pretty self explanatory. Feel free to experiment...

You should now see your newly created class in an editor window.

Views and editors
Eclipse makes a distinction between views and editors. An editor is a window which directly displays editable content, such as the Java editor above. When you make a change in an editor, the change doesn't take effect until you save the editor. You can tile and layer editors in various ways by dragging their title bars. To see a list of all open editors, press Ctrl-E.
A view is a window which displays a more abstract representation of content, for example the Package Explorer, Problems, and Outline views above. Changes made in a view take effect immediately. You can dock, layer, and tile views is various positions by dragging their title bars. To open a new view, select Window->Show View->Other...
To toggle maximisation of the current view or editor, double-click its title bar or press Ctrl-M.


Now you can start writing code.
Eclipse has so many useful Java development features, I can't do justice to them here. I'll refer you back to Java Development Tooling Tips and Tricks and Base Eclipse Platform Tips and Tricks for a small taste of what's possible. Go ahead and experiment!

Sharing preferences


Almost all of Eclipse's features are in some way configurable via preferences. You may want to share your preferences with other people, for example to enforce a consistent coding style. You may also want to have several workspaces, and use consistent preferences across all of them.
You can export your preferences using the Export... button on the main Preferences dialog (Window->Preferences). This will export your preferences in XML format. Preferences exported in this way can be imported using the Import... button.

Multiple projects, and the Order and Export tab


If required, you can create multiple Java projects and introduce build dependencies between them. Create a new Java project called "other", then look at its build path (right-click, Properties, Java Build Path). You'll see an entry for your original project as show below. Check this to introduce a build dependency.

Now look at the build path of your original project. The "Order and Export" tab contains a list of all entries on the project's classpath. If any of them are checked, they are exported, which means they are automatically added to the classpath of any project which is dependent on this project. For example, if we were to check the joda-time-098.jar entry, it would be automatically added to the classpath of the "other" project.

Note that you can select any entry and use the "Up" and "Down" buttons to change its order in the classpath.