Java Workshop
 

The Eclipse IDE

Jargon

Command
An instruction given to the computer, by means of a keyboard, punch card, mouse, voice command, or other method.
Console
The command prompt window that rece ves output from a System.out.print call.
Drive
A device that spins disks or tapes in order to read and write data; for example, a hard drive, floppy drive, CD-ROM drive, or tape drive.
Error
A mistake made by a software developer that introduces a defect in a computer program.
Generate
The automated creation of source code.
IDE
Integrated Development Environment, an IDE combines the editor, compiler and other useful tools in the same software package. Its advantage is that when a program with syntax errors is compiled, the programmer sees the error messages and the original program at the same time -- this makes debugging much easier.
Refactor
Refactoring is the process of rewriting written material to improve its readability or structure, with the explicit purpose of keeping its meaning or behavior.
Syntax
The rules by which the words in a program are combined to form commands to a computer.
Syntax Highlight
Syntax highlighting is a feature of some text editors that displays text, especially source code, in different colors and fonts according to the category of terms. This feature eases writing in a structured language such as a programming language or a markup language as both structures and syntax errors are visually distinct.
Zip
To zip a file is to compress it into an archive so that it occupies less disk space.

Introduction

Often newcomer's to programming will find editing source files in a text editor to be tedious. Seasoned developers hardly ever use a text editor either. An Integrated Development Environment (IDE) provides the functionality to edit source code, compile it and run the source code all from within a single application.

Eclipse is an excellent open source IDE for Java. It is developed by IBM and written in Java itself. Eclipse provides a wide range of capabilities and features; we will look at this useful IDE in this chapter.

You can get Eclipse @ www.eclipse.org

Installing The Eclipse SDK

The first thing we need to do is to install Eclipse.

Note
Eclipse is a memory intensive application may take a while to load, depending on how much memory your system has.

Installing on Windows

  1. Extract the eclipse archive to the C:\ drive using a program like Winzip or ZipGenius. You should end up with a folder in the C: drive named eclipse.

    Extract the archive
  2. The eclipse program is in the eclipse folder and is a file named eclipse.exe. You can double click this file to run the program. You might want to create a shortcut to this file on your desktop or in your start menu.

    Extract the archive

Installing on Linux

Note
You should be logged in as root to perform this installation.
  1. Copy the Eclipse SDK archive to the /usr/local directory.

    cp eclipse-SDK-3.0-linux-gtk.zip /usr/local/
  2. Change into the /usr/local directory and extract the archive.

    cd /usr/local/
    unzip eclipse-SDK-3.0-linux-gtk.zip
  3. Edit the file named java.sh in the /etc/profile.d directory and add the following to it.

    Note
    If you installed java according to chapter 1 you should have a file named java.sh in the /etc/profile.d directory. If not, now would be an opportune time to create it.
    export ECLIPSE_HOME=/usr/local/eclipse
  4. Now add ECLIPSE_HOME to the PATH variable, save the file and exit your editor.

    PATH=${PATH}:${ECLIPSE_HOME}

    Log out and then log in again for the changes to take effect.

  5. Run Eclipse: in a terminal type

    eclipse
  6. In the dialog that asks you to select a workspace click the OK button. After eclipse has started, you will get a welcome screen.

Creating a Hello World Application

The best way to learn about Eclipse and its features is to start using it; we will therefore create a simple Hello World application using it.

Create a New Java Project

  1. Start eclipse with the command:

    eclipse
  2. Select a workspace from the dialog and ensure to check the box that makes it the default workspace.

  3. In the welcome screen, click on the "Workbench" icon.

    01
  4. From the File menu, select New and then Project.

    File > New > Project
  5. In the New Project Dialog, click Java Project and then click the Next button.

    02
  6. In the New Java Project Dialog enter Hello World as the Project name and click the Finish button.

    03
  7. A dialog will pop up asking you to confirm the switch to the Java Perspective. Check the checkbox that reads Remember my decision and then click the Yes button.

    04
  8. In the Java Perspective, in the left side of the screen is the Package Explorer. In it you will see the Project named Hello World. Make Sure it is selected.

    05

Create a New Class

  1. Select File from the menu and click New. Select Class.

    File > New > Class
  2. In the New Java Class dialog enter HelloWorld as the name of the class, ensure that the public static void main(String[] args) checkbox is selected and then click the Finish button.

    06
  3. You'll notice in the source code editor that the class has already been created for you.

    07

Add Source Code

  1. In the main method, type

    System.

    You'll notice a popup, prompting you to select a member of the System class. Select the out member.

  2. Append the following to the line:

    .println("Hello World!")
  3. You'll notice a red squiggly line under the closing bracket. This indicates that there is an error in your code. Add a semi colon (;) and it will disappear.

    08

Save the Code

  1. Select Save from the File menu to save the changes to the source code. You'll notice that Eclipse compiles source code automatically, every time that you save it.

    File > Save

Run the Program

  1. Right Click on the HelloWorld.java file in the Package Explorer and select Run followed by Java Application from the pop up menu.

    Run > Java Application
  2. You'll notice the output in the console.

    09

Some Advanced Features

Configuring Some Preferences

We will now change a few of the default preferences.

  1. Select Preferences from the Window menu.

    Window > Preferences
  2. Click the arrow next to Workbench to expand the sub categories and select Colors and Fonts. In the right pane expand Java and then Java Editor Text Font. Click the Change button, and change the font according to your preferences.

    10
  3. In the left pane, expand the Java category and then the Code Style sub category. Select Code Templates. In the right pane select Comments and then Types. Click the Edit button and change the comment to your preferences.

    11
  4. Now select Code and then the New Java files sub category. Click the Edit button and change the comment according to your preferences.

    12
  5. In the left pane select Editor and select the Show line numbers check box.

    13
  6. Select the Syntax tab in the right pane and change the color of the keywords from mauve to blue. Click the Apply button.

    14
  7. Click the Ok button. You'll notice that the changes have taken effect in the source code editor.

    15

Create a New Project

We will now create a project outside of eclipse and then import it into eclipse.

  1. In your home directory, create some directories:

    mkdir ~/test
    mkdir ~/test/src
    mkdir ~/test/classes
  2. In Eclipse, create a new project by selecting New from the File menu and then select Project.

    File > New > Project
  3. In the dialog, ensure that Java Project is selected and click the Next button.

  4. In the next dialog, use Test as the project name. Then click the radio button that reads Create project at external location. Click the browse button and select the test directory in your home directory. Be sure to click the Next button.

    16
  5. In the source folders list, select Test and click the remove button. Click the Add Folder button and select the src directory. Click the OK button.

    17
  6. Click the browse button to change the Default output folder and select the classes directory. Click the Ok button.

    18
  7. The resulting changes should be as follows:

    19
  8. Click the Finish button.

Create Some Packages

You'll notice the Test project in the Package Explorer. We will define a package for our project.

  1. In the package explorer expand the Test project.

  2. Right click on the src folder in the package explorer and select New and then Package. Add test as the package name and click the Finish button.

    20
  3. You will now see the empty package in the Package Explorer.

Create a New Class

We will create a simple class in the package we have just created..

  1. Right click on the Test package. Create a new class named Cat by selecting New and then Class from the pop up menu.

    21
  2. Add two variables, one named age of type int and one named name of type String.

    22
  3. Add a simple constructor to initialize the variables.

    23

Generate Getters and Setters

Now we will use eclipse to generate getter and setter methods for our class.

  1. Select Generate Getters and Setters from the Source menu.

    Source > Generate Getters and Setters
  2. In the dialog click the Select All button followed by the Ok button.

    24
  3. You'll notice that the get and set methods were created.

    25
  4. Don't forget to save the file.

Refactoring

We would like to change the name of the package from test and have all changes reflected. First we will create a main program that will use our class.

  1. Create a new package named com.acme.test.main

  2. Create a program called Main in the new package.

    26
  3. Add the following code to the main method.

    Cat kitty = new Cat()
  4. You will again notice some squiggly red lines. In the gutter next to the line numbers you will see a hint icon. Click on it.

    27
  5. In the pop up hint you will be prompted to import the Cat class. Do so, by clicking on the suggestion. You will notice that an import statement has been added to your code, namely: import test.Cat. Save the class.

    28
  6. Right click on the test package in the Package Explorer and select refactor from the menu. Select Rename from the sub menu.

    Refactor > Rename
  7. Rename the package to com.acme.test.pets .

    29
  8. If you look at the main class, you'll notice that the import statement has changed to match the new package name, ie: import com.acme.test.pets.Cat!

    30

Installing a Plug-In

Eclipse is designed in such a way that third party developers can create plug-ins for it. One such plug-in allows you to edit XML files: it provides syntax highlighting for XML and code completion.

Installing a plug-in in Eclipse is a simple and painless process. All plug-ins are distributed in zip compressed archives, much like eclipse. All you have to do is extract the archive in the correct directory within Eclipse and it should work out of the box.

For this installation, we're using the XML buddy plug-in form here.

  1. If you open an XML file in Eclipse you will notice that Eclipse uses the default text editor for XML files. This allows you to edit the files but it doesn't provide features common to most IDE's. In this case, installing a plug-in will help.

    31
  2. Close Eclipse before proceeding.
  3. Extract the archive to location of your choice. This results in a folder named com.objfac.xmleditor_version.
  4. Move this folder into a folder named plugins within the eclipse folder.

    32
  5. Start Eclipse and open an XML document, you should see that it is now syntax highlighted.

    33
Note
Installing a plug-in that is not for your version of Eclipse will not work. You should make sure you get the plug-in that is specifically for your version of Eclipse.

Summary

Eclipse is an advanced and feature rich IDE for the Java language. It is simple to install and easy to learn to use. We will be using this valuable tool in the next few chapters in this book.

Exercise Set

Multiple Choice

  1. Eclipse is written in which language?

    1. Pascal
    2. C
    3. C++
    4. Java
    5. Delphi
  2. Eclipse is:

    1. Proprietary Software
    2. Commercial Software
    3. Open Source Software
    4. Shareware
    5. Freeware
  3. Which is not a feature of Eclipse

    1. Eclipse allows easy refactoring of code.
    2. Eclipse is available for many platforms.
    3. Eclipse has Apache Ant integrated into it.
    4. Eclipse requires you to compile source code after it is saved.
    5. Eclipse has JUnit integrated into it.
  4. Eclipse allows you to:

    1. Generate new classes and interfaces quickly and easily.
    2. Use third-party plug-ins.
    3. Generate getters and setters.
    4. Refactor code.
    5. All of the above.
  5. Eclipse is available for:

    1. Windows
    2. Linux
    3. Sun Solaris
    4. 1 and 2.
    5. 2 and 3.

Exercises

  1. Install the Eclipse on your own computer.
  2. Create the Hello World program described in this chapter.
  3. Import your existing projects into Eclipse.
  4. Configure the Preferences of Eclipse according to your taste.
  5. Install the plug-in as described in this chapter.

Programming Exercises

  1. Complete the following programs using Eclipse:
    1. A program that calculates how much is left of a bank loan. Input parameters are initial loan amount, interest rate, payment per month and current month.
    2. A program to calculate the average of a list of numbers.
    3. A program that calculates a student's grade from grades, each counting an equal percentage.