COSC 1047 Eclipse Tutorial
Introduction to Computer Science II
January 2009

This tutorial assumes you are working in the lab or have successfully installed Eclipse on your home computer. Instructions are given below for installing Java if necessary and for installing Eclipse which requires that Java be installed first.

We will create an Eclipse project called first and a package containing two classes, a Point class for 2 dimensional geometrical points and a tester class PointTester that has a main method we can run to test the Point class.

We will also show how to export the project as a zip file and how to import a project stored in a zip file.

Running Eclipse for the first time

The Eclipse IDE organizes your java classes and packages into workspaces and projects. Each workspace can contain a number of related projects.

When you run Eclipse for the first time you are prompted to create a workspace called workspace in your home directory. You can accept this or use any directory (I use c:/eclipse34/e-workspaces/workspace since I have many workspaces organized inside a subdirectory called e-workspaces. For our course you only need one workspace).

After you have chosen your workspace the Eclipse IDE appears and you can close the Welcome window that appears by clicking the x in its top left corner.

You will now see several windows. The important ones are the "Package Explorer" on the left, the editor window in the middle, and the tabbed window at the bottom. On the right is an "Outline" window that shows classes and method names.

When you run Eclipse again you can use this workspace by default.

Creating your first project

Java organizes your classes into a package hierarchy, such as the built-in packages java.lang, or java.util.

Eclipse organizes your Java classes into projects in each workspace. You can think of a project as a collection of related Java packages each containing one or more classes that represent a Java application. For example, each assignment of our course will correspond to Eclipse projects called assignment1, assignment2,... and the solutions will correspond to projects called assignment1s, assignment2s,...

It is important to note that the concept of workspaces and projects are Eclipse IDE concepts not Java concepts: You will not find project names inside Java source code. On the other hand the concept of a package is definitely a Java concept and classes will have a package statement as the first line of the class.

We create a project called first as follows:

Creating the geometry package in the project

Now we create a package called geometry in our project as follows: The package name will now appear inside the project name in the "Package Explorer" window.

Creating the Point class in the geometry package

Now we create the Point class as follows The editor window will now show the class template
package geometry;

public class Point {

}
Our convention is to put braces underneath each other so you can change it to
package geometry;
public class Point
{

}
Later you can learn how to customize the editor to use this formatting of the braces by default.

Now complete the Point class to obtain the following class.

package geometry;

/**
 * A simple class to represent immutable geometrical
 * points in the plane.
 */
public class Point
{
   private double x;
   private double y;

   /**
    * Construct a point with given x and y coordinates.
    * @param x the x coordinate of this point
    * @param y the y coordinate of this point
    */
   public Point(double x, double y)
   {
      this.x = x;
      this.y = y;
   }

   /**
    * Construct a default point (0,0)
    */
   public Point()
   {
      this(0.0, 0.0);
   }

   /**
    * Return the x coordinate of this point.
    * @return the x coordinate of this point
    */
   public double getX()
   {
      return x;
   }

   /**
    * Return the y coordinate of this point.
    * @return the y coordinate of this point
    */
   public double getY()
   {
      return y;
   }

   /**
    * Return a string representation of this point.
    * @return a string representation of this point of
    * the form Point[x,y]
    */
   public String toString()
   {
      return "Point[" + x + "," + y + "]";
   }
}

When finished click the save button on the main toolbar (picture of a floppy disk).

When you are learning the Eclipse editor it is convenient to turn off folding which is only useful for large classes.

To do this right click in the left margin of the editor window, choose "Folding" and remove the check mark from the "Enable folding" menu choice

A nice feature of Eclipse is that it compiles your code as you type. Errors are indicated in red underlining in the code and red x's in the left margin or the editor and red boxes in the right margin.

Make some errors in your code to see the error messages: click on a red box or move cursor over underlined text.

Creating the PointTester class in the geometry package

Now create the PointTester class as follows The editor window will now show the class template
package geometry;

public class PointTester {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub

   }
}

Fill in the remainder of the source code so that it looks like

package geometry;
/**
 * A class to test the Point class
 */
public class PointTester
{
   public void doTest()
   {
      // test constructors

      Point p = new Point();
      System.out.println(p);
      Point q = new Point(1,2);
      System.out.println(q);

      // Test get methods

      System.out.println("x coord of q is " + q.getX());
      System.out.println("y coord of q is " + q.getY());
   }

   public static void main(String[] args)
   {
      new PointTester().doTest();
   }
}
Note that the editor is a multi-tabbed editor.

Running the PointTester application

Run the PointTester application as follows: The following results are displayed in the console window:
Point[0.0,0.0]
Point[1.0,2.0]
x coord of q is 1.0
y coord of q is 2.0

Generating and Viewing the Javadoc for your project

To generate java documentation it is necessary to tell Eclipse where to find the javadoc program. It is located in the JDK (Java Development Kit). For example,
C:\Program Files\Java\jdk1.6.0_03\bin\javadoc.exe
Now create the java documentation for project first as follows: You can view the Java documentation as follows

Exporting the project as a zip file (archive)

For course assignments you will need to export the assignment as a zip file and submit it.

The project first that we have created can be exported as a zip file (archive) as follows:

Note: the zip file can have any name but it is usually given the same name as the project being exported.

Importing a project stored in a zip file

When you begin an assignment you will need to know how to import a project stored in a zip file.

An Eclipse project that was exported as a zip file can be imported into Eclipse. The zip file normally has the same name as the project.

Before importing a project make sure it doesn't already exist in your workspace. If it does, delete or rename it first: see below for how to delete or rename a project.

To illustrate this we have included the zip file hello.zip. Right click this link and choose "Save target as..." to download it and save it somewhere, on your desktop for example.

This file contains a simple project called hello which can be imported into your Eclipse workspace as follows.

If you don't need the zip file anymore you can delete it.

Installing Java and Eclipse

If you don't have Java or Eclipse installed follow these instructions noting that Java must be installed before you can run Eclipse.

Installing Java

If you don't have the Java software Development Kit (JDK) installed or have an earlier version you can install version 6 as follows. Download the executable file
http://www.cs.laurentian.ca/badams/jdk-6u3-windows-i586-p.exe

Run this file to install Java and accept the defaults. This installs the Java software Development Kit version 6 in the directory
C:\Program Files\Java\jdk1.6.0_03
Now you can install the Java API documentation as follows. Download the zip file
http://www.cs.laurentian.ca/badams/jdk-6-doc.zip
You need to unzip it into the directory
C:\Program Files\Java\jdk1.6.0_03
The documentation will be located in the directory
C:\Program Files\Java\jdk1.6.0_03\docs
You can now create a shortcut on your desktop to the API documentation:
C:\Program Files\Java\jdk1.6.0_03\docs\api\index.html

Installing Eclipse

To install the latest version of Eclipse (version 3.4) first make a directory (folder) to hold the files (I use c:\eclipse34).

Download the zip file
http://www.cs.laurentian.ca/badams/eclipse-SDK-3.4-win32.zip

and unzip it into the directory that you chose.

WARNING: The built-in windows decompression may not work. Use Winzip or 7-zip if you have problems.
Inside this directory you will find the directory eclipse containing the eclipse software. Inside this directory you will find the executable file eclipse.exe that runs eclipse. Make a shortcut to this file in your desktop and name it eclipse34

Other tasks

Renaming a project

Deleting a project

Closing a project

Closing is not the same as deleting. For example if your workspace contains many projects then each time you edit and compile a project all projects will be checked. This can slow down Eclipse so it is best to close projects that are not being used. To close a project:

Opening a project

To open a project that has been closed

Customizing the Java editor code style

The editor formatting can be customized. For example, we can specify that tabs be replaced by spaces using an indentation of 3 spaces. We can also specify that braces be automatically lined up underneath each other. This can be done as follows: