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.
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
workspacein your home directory. You can accept this or use any directory (I usec:/eclipse34/e-workspaces/workspacesince I have many workspaces organized inside a subdirectory callede-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.
Java organizes your classes into a package hierarchy, such as the built-in packagesjava.lang, orjava.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 calledassignment1s,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
packagestatement as the first line of the class.We create a project called
firstas follows:
- Select "File" on the main menu and select "New" and "Java Project"
- In the "Project Name:" field enter the name of the project. For this tutorial use
firstas the project name.- In the "Contents" group of radio buttons make sure the radio button "Create new project in workspace" is selected
- In the "Project layout" group of radio buttons make sure that the radio button for "Use project folder as root for sources and class files" is selected
- Click the "Finish" button to finish creating the project. It will appear as an item in the "Package Explorer" window.
geometry package in the projectNow we create a package calledgeometryin our project as follows:The package name will now appear inside the project name in the "Package Explorer" window.
- Select project
firstin the "Package Explorer" window.- From the "File" menu select "New" and choose "Package".
- In the "Name" field type
geometryas the package name.- Click the "Finish" button when done.
Point class in the geometry packageNow we create thePointclass as followsThe editor window will now show the class template
- Select the
geometrypackage in the "Package Explorer" window.- From the "File" menu select "New" and choose "Class". The "Source folder:" field should now contain the project name and the "Package:" field should contain the package name.
- Fill in the "Name:" field with the class name
Point. You can optionally create amainmethod but we don't want one in thePointclass.- Click the "Finish" button when done.
package geometry; public class Point { }Our convention is to put braces underneath each other so you can change it topackage 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
Pointclass 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
savebutton 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.
PointTester class in the geometry packageNow create theNote that the editor is a multi-tabbed editor.PointTesterclass as followsThe editor window will now show the class template
- Select the
geometrypackage in the "Package Explorer" window.- From the "File" menu select "New" and choose "Class". The "Source folder:" field should now contain the project name and the "Package:" field should contain the package name.
- Fill in the "Name:" field with the class name
PointTester. Have amainmethod created in thePointTesterclass by checking its check box.- Click the "Finish" button when done.
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(); } }
PointTester applicationRun thePointTesterapplication as follows:The following results are displayed in the console window:
- Select the
PointTesterclass in the "Package Explorer" or the editor.- From the "Run" menu select "Run As" and select "Java Application".
Point[0.0,0.0] Point[1.0,2.0] x coord of q is 1.0 y coord of q is 2.0
To generate java documentation it is necessary to tell Eclipse where to find thejavadocprogram. 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 projectfirstas follows:You can view the Java documentation as follows
- Select project
firstin the "Package Explorer".- From the "Project" menu select "Generate Javadoc..."
- If the "Javadoc command" field is blank you need to enter the full path to
javadoc.exeas mentioned above. Instead of typing it you can use the "Configure..." button to navigate tojavadoc.exe- Note that the "Destination:" field shows that the javadoc files will be saved in a
docdirectory inside the project directory. We can use this default location.- Click the
Finishbutton
- In the "Package Explorer" select project
first.- From the "Navigate" menu select "Open External Javadoc".
For course assignments you will need to export the assignment as a zip file and submit it.The project
firstthat 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.
- In the "Package Explorer" select project
first.- From the "File" menu select "Export..."
- Open "General" and select "Archive File".
- Click the "Next>" button and verify that project
firstis checked.- In the "To archive file:" field use its browse button to navigate to the directory in which you want to save the zip file. A common place to store it temporarily is your desktop.
- In the "File name:" field enter the file name
first.zipin our case.- Click the "Save" button and the "To archive field:" will be filled in.
- Click the "Finish" button and the zip file will be created.
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
hellowhich can be imported into your Eclipse workspace as follows.If you don't need the zip file anymore you can delete it.
- Download the zip file somwhere.
- From the "File" menu select "Import..."
- Select "General"
- Select "Existing Projects into Workspace".
- Click the "Next>" button.
- Click the "Select archive file:" radio button.
- Use its "Browse..." button to navigate to the zip file. When done the
helloproject should appear in the "Projects:" list with a check mark- Click the "Finish" button and the
helloproject should appear in the "Package Explorer" window. It should contain a package calledtestand a class calledHelloWorld.javawhich you can run.
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 directoryNow you can install the Java API documentation as follows. Download the zip fileC:\Program Files\Java\jdk1.6.0_03You need to unzip it into the directoryhttp://www.cs.laurentian.ca/badams/jdk-6-doc.zipThe documentation will be located in the directoryC:\Program Files\Java\jdk1.6.0_03You can now create a shortcut on your desktop to the API documentation:C:\Program Files\Java\jdk1.6.0_03\docsC:\Program Files\Java\jdk1.6.0_03\docs\api\index.html
To install the latest version of Eclipse (version 3.4) first make a directory (folder) to hold the files (I usec:\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 directoryeclipsecontaining the eclipse software. Inside this directory you will find the executable fileeclipse.exethat runs eclipse. Make a shortcut to this file in your desktop and name iteclipse34