3. Development Tools

Interactive Development Environment (IDE)

Author: Scott Franson and Russ Bateman

Objective: Install Eclipse IDE; write and debug an application.

or

Objective: Install NetBeans IDE, much easier to use; write and debug an application. Skip to links at bottom of this page.


Introduction

Nota bene: This material was largely written in the Fall of 2005 and is no longer up to date. Since then, Eclipse has made major strides in support for web applications development in the form of its Web Tools Platform. For a better overview and detailed installation instructions, consult this more recent tutorial written in Winter/Spring 2008.


Java was developed for, and is used as a rapid development environment. As such, simple editors and build tools are insufficient to meet the requirements of companies developing software for the web. Integrated development environments (IDEs) and other advanced development tools are necessary. Despite its price (free) the Eclipse IDE is one of the premiere development tools in use today. This subject will introduce you to the Eclipse IDE and walk you through the development of a simple Java application.

As useful as IDEs are, they sometimes cannot meet the specific needs of a project. Other tools are required to customize the project environment for efficient builds and deployments of project files. Next to a good editor and version control system, a good build tool is a developer’s best friend. ANT and it’s XML files is the Java equivalent of make and Makefiles. With ANT a developer can specify exactly how files are compiled and packaged. ANT can do it all from creating directories to deploying entire applictiaons: if the developer has the knowledge and skill to put it all together. We will introduce ANT and demonstrate how it can be used to build your application.

As good as ANT is as a development tool it requires a significant amount of knowledge and attention from a developer. As such, it is merely an incremental step away from make and does not fit well with the paradigm of rapid development and deployment of web applications that is expected of J2EE developers. Enter Maven. Maven is properly called a “project management tool” and is used to free the developer from the necessary knowledge and attention required of ANT. Everything you can do in ANT you can do in Maven, but it reduces the size and complexity of build files by a significant degree. In this tutorial, we will take the same project, built by ANT, and build it using Maven. We will then integrate Maven into the Eclipse IDE and build our JDBC application into a JAR file using Eclipse and Maven together.

The Eclipse IDE

Though free and open source, the Eclipse IDE is one of the premiere development environments for J2EE development. Its elegant interface places everything a developer needs: project files; file editors; documentation pop-ups; syntax checking and coloring; built in compilers and debuggers... It will beautify your source, display parameters to the method call you are using, and even automatically compile your source as you create it. Those old-fashioned, A-type personalities among us (including myself) who feel the need to have their keystrokes touch every aspect of the project’s files may be resistant to the features of modern IDEs; they even generate code for you! Yet these tools and features are necessary to the developer given the expectation of rapid development and deployment of J2EE applications. We will walk through the assignment to create a HelloWorld application delivered in a JAR file.

Installation and Configuration

Prerequisites: before you download and install Eclipse, make sure your development workstation has the latest Java SDK installed:

http://java.sun.com/j2se/1.5.0/download.jsp

Below is the link to the Eclipse download page. You only need the “SDK 3.1,” though be warned it is large (~100MBytes). The package is delivered either as a .zip file (Windows) or as a tar ball (Linux). To install, merely extract the package into the directory of your choice. Be certain to check the Use Folder Names. If you unzip it to the root of c:, you will get a directory immediately off that volume, named eclipse. This is probably what you want.

http://www.eclipse.org/downloads/index.php

Under the eclipse directory is the Eclipse executable. You may want to create a shortcut on the desktop to launch it.

Once Eclipse is launched, it will ask you select a workspace. Click OK to continue. In Eclipse, you can have multiple workspaces. Each workspace contains multiple projects. You can only work with one workspace at a time. (You are not required to place your project files under your workspace directory, though Eclipse assumes so by default.)

If the Welcome screen is displayed, dismiss it by clicking on the X on the Welcome tab under the menu row. (You can display the Welcome screen any time via Help | Welcome.)

The first thing you want to do is verify the JDK is installed and available via Eclipse. Select Window | Preferences..., then expand the Java iten and click on Installed JREs.

If no JDK (JRE) is not listed, click Add if you know the location of the JDK (JRE), or Search to search for it. (The latest JDK is installed by default under C:\Program Files\Java\jdk1.5.0_05.)

With the JDK available, we are ready to develop Java applications.

Creating a Project

We will create a project for our HelloWorld appliction. Select File | New | Project to bring up the Project page. Select Java Project and click the Next button.

Give your project a name, such as My First Hello World. Under Project layout, select Create separate source and output folders and click the Configure default link.

To conform to our standard directory structure we want a source folder to be named src and the output (build) folder to be named target. So, change the output folder name to target and click OK.

Click Finish to create the project.

In Eclipse, the subwindows are called views. These views are tabbed so several “views” can be behind the currently visible windows. We want the Navigator view; from the drop-down menu select Window | Show View | Navigator.

The Navigator view should now be visible on the left side of the workspace. To finish making our directory structure, make sure the Navigator view is visible by selecting Window | Show view | Navigator. Right-click on “My First Hello World,” choose New..., then Folder, type in src for Folder Name near the bottom of the box and click on Finish. Repeat this procedure for the target directory. To create our remaining folders expand the project in the Navigator and right-click on the src directory and select New | Folder.

For Folder name, type main/java to create our folders.

Next, we need to add our source directories to the build path of the IDE so it will know where our source files will be. In Navigator, right-click on the project and select Properties. Click on Java Build Path, then the Source tab. Now click the Add Folder button and expand the src directory to show the main, then java subdirectories. Click the check box of the java directory to add the directory structure to the project’s build path. If a confirmation dialog comes up, answer it with Yes, etc.

Click OK to dismiss the project properties page.

Our project is now set up and ready for source files. To add a source file, expand the src directory to the java directory in Navigator and right-click. Select New | Class to create our main Java class.

In the New Class page, under Name:, type the name of the class, "HelloWorld" and select the option to create the main( String[] args) method.

When you click Finish, the IDE will display a new view, the Java editor, with the stub of our new class file.

At this point, if you care to do so, you might change the rather peculiar indentation and formatting practices that are the default in Eclipse through the following Eclipse menu progression:
Window | Preferences | Java | Code Style | Formatter

All we must now do is modify the main method to printout “Hello world!.” As you type in the command to print “Hello world!,”

System.out.println("Hello world!");

You will see a number of things the editor will do for you: it will indicate errors, offer context options for the out stream and the println methods and will open parentheses and quotes for you. All of these things are configurable.

Once you have finished entering the code it should look like this:

Note the asterisk next to the file name that indicates that the file has been modified and needs to be saved.

Select File | Save, and if the menu option Project | Build Automatically is set, the IDE will compile your source for you.

You will also note the comment in the main method, TODO. Eclipse will track for you things that you must do when it generates code for you. These “tasks” are visible via the Tasks view. Select
Window | Show View | Other | Basic | Tasks
to display the Tasks view. When you fill out the method stub you can mark the task completed. (Don’t forget to remove the TODO comment from your source; if you leave it in it will show up in reports.)

Debugging a Project

Now that the application builds, we can investigate the integrated debugger within Eclipse. From the Run menu, select Debug. The Debug page comes up.

Right-click on Java Application and choose New. You should get a new project named “HelloWorld.”

If we had an argument or several, we could list them using the Arguments tab in the Debug dialog now open. However, since application does not use any arguments, simply check Stop in main and click the Debug button at the bottom of the dialog. Answer Yes to the confirmation dialog (and check Remember my decision). This opens what is called the “debug perspective.” (We just left the Java perspective.) The top right has icons for the currently open perspectives.

You should now have a screen that looks similar to this:

We can see the Debug tab, which contains the call stack and the controls to step through the code. Further to the right is the Variables tab, containing the local variables to the method; and the Breakpoints hidden beneath. Below is the editor tab with our source file and the Outline tab to the right. At the bottom is the Console tab where program output will be displayed. Click the Step Over control to step over the println call and see the output displayed in the Console tab.


Click the Java perspective button to go back to where we started.


Creating a JAR File

Now that we are sure our application works we can add a JAR file to our project to prepare it for “deployment.” From the File menu select Export; select a JAR File and click Next. This brings up the JAR Package Specification page. For simple projects like this it is simply a matter of checking the Export generated class files and resources option and give our JAR file a name (HelloWorld.jar). As the export destination is relative to our workspace (and not our target directory) we need to add to the path our project root directory and the target directory. Therefore, click on Browse and navigate to the target directory under your project in the work area.

Click on Next for the JAR Packaging Options page. We will accept the default values here, so click Next for the JAR Manifest Specification page. Each JAR file contains a manifest file that describes the contents and, importantly for us, specifies the class containing the main method as the application entry point (enter this in the Main class field). This allows users of our JAR file to launch our application by simply giving the command java -jar HelloWorld.jar.

Click Finish to continue.

If we have not set the IDE to automatically build, we must build the JAR file by selecting Project | Build All. We should now see the JAR file in the target directory using the Navigator. To test the jar file, we can bring up a console and navigate to the target directory and test the application using the method described above.


Links


Assignment

Follow the steps to setting up Eclipse or NetBeans in one of the tutorials in the links above.