Untitled
UNCP Home Page
Lillie Home Page
Eclipse Tutorial Fall 2009
Office Information
002 Instructor: Dr. Charles W. Lillie
Office: 1219 Oxendine
Telephone: 910-521-6415 email: lilliec@uncp.edu
Office Hours: MWF 8:30-9:30, MW 2:30-3:30

Introduction to Eclipse 3.4 (Ganymede) - Tutorial

Abstract

This is a brief introduction to using Eclipse as a Java IDE. It contains a short description on how to install Eclipse, discusses features of Eclipse which should increase the programmers productivity, and lists the most important keyboard shortcuts.

1. Getting started

1.1. Installation

Download Eclipse Ganymede from the website http://www.eclipse.org/ and unpack it to any directory but I recommend putting it in folder C:\Programs\Eclipse. No installation procedure is required.

I suggest you download Eclipse IDE for Java Developers (85 MB) available for download here

Eclipse requires Java version 1.5 or later.  I recommended Java 6 (also known as Java 1.6) available for download here.  Be sure to download Java SE Development Kit (JDK) 6 

1.2. Start Eclipse

To start Eclipse double-click on the file eclipse.exe in your installation directory.

The system will prompt you for a workspace. The workspace is the place where you store your Java projects.


Eclipse Workspage Launcher
Select a suitable folder and press Ok.
Eclipse will start and show the Welcome page.


Eclipse Welcome


To close the welcome page click the little x in the Welcome tab.

1.3. Software Updates and Installation of additional Plug-ins

Eclipse provides functionality via so-called plug-ins. Eclipse Ganymede contains a Software Update Manager which allows you to update existing plug-ins and to install new plug-ins. The update manager is located in the menu Help Software Updates


Software Updates and Add-ons

Click on "Check for updates...". The system will verify the availability of the installed plug-ins updates.

Select the "Available Software" tab to select additional software to install. For example, to make all packages distributed with Ganymede available, click manage sites and select the Ganymede Update Site. Open the list to see the installable packages.

2. Eclipse Overview

2.1. Workspace

The workspace is the physical location (file path) you are working in. You can choose the workspace during startup of eclipse or via the menu (File Switch Workspace Others).

All your projects, sources files, images and other artefacts will be stored and saved in your workspace.

2.2. Define workspace at startup time

To predefine the workspace you can use the startup parameter -data path_to_workspace, e.g. if your eclipse is located in c:\eclipse use the following to set the workspace to c:\temp. Please note that you have to put the path name in quotes ("). For example:

c:\eclipse.exe -data "c:\temp"

2.3. Eclipses User interface Elements

Eclipse provides perspectives, views and editors. Views and editors are grouped into perspectives.

2.4. Perspective

A perspective is a visual container for a set of views and editors (parts).

You can change the layout within a perspective (close / open views, editors, change the size, change the position, etc.)

Tip

If you "misconfigured" your perspective and would like to reset it to the original state use the menu "Window" →   "Reset Perspective" to reset the perspective.

Eclipse allows you to switch to another perspective via the menu Window→ Open Perspective  →  Other.

2.5. View

Views provide information for a given task. A view is typically used to navigate a hierarchy of information, open an editor, or display properties for the active editor.

2.6. Editor

Editors are meant for for the primary focus of attention and shows the main content of your application.

2.7. Editor / View comparison

The main differences between editor and views are not technical but semantic differences. Use editors for a task if this is a primary task.

Technical differences:

  • Editors are shared between perspectives, e.g. if you close an editor in one perspective it is closed in all perspectives.

  • You can asked for the active editor even if the editor is not in focus. This makes updates of the active editor / access to the editor easier.

To describe views and editors take the example of an email client. The navigation within the email client would be done via views, while the part of the application which is used for writing the email would be performed via an editor.

3. Create your first Java program

3.1. "Hello Eclipse Ganymede!" console program

The following will describe how to create a minimal Java program using Eclipse.

Select from the menu File  →  New →  Java project. Enter "FirstGanymede" has the project name. 


Select “Create separate source and output folders.

New Java Project

Press finish to create the project. 


A new project is created and displayed as a folder.

Java - Ecl;ipse SDK

Open the folder "FirstGanymede", select the folder src, right mouse click on it and select create package.

Create package "mypackage".

Java - Eclipse SDK

Type the package name in Name: box.

New Java Package

Right click on your package and select New  →  ClassJava - Eclipse SDK

Create MyFirstClass, select the flag "public static void main (String[] args)"

New Java Class

Enter the following code.

				
package mypackage;
public class MyFirstClass {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Hello Eclipse Ganymede!");
}
}


Now run your code.

Java - FirstGanymede/src/mypackage/MyFirstClass.java - Eclipse SDK


Finished! You should see the following output in the Console.

Console

4. Productive Usage of Eclipse

4.1. Content assist

The content assistant can be invoked by Ctrl + Space and allows you to get input help for typed values. For example type syso and then press [Ctrl + Space] and it will be replaced by System.out.println("").

If you press Enter during code assistant then the cursor will go to the end of the line.

Allows you to define Java types based on the camelCase notation. So you can type NPE and press [Ctrl+Space] to get the Types which have the three capital letters included, e.g. NullPointerException.

The content assistant is also available for the definition of variables. For example, define a variable "private String" and press [Ctrl+Space] to define the name automatically. By default it will use the name of the type.

4.2. Quick Fix

Whenever there is a problem Eclipse will underline the problematic place in the coding. Select this and press (Ctrl+1)

For example type "myBoolean = true;" If myBoolean is not yet defined, Eclipse will highlight it as an error. Select the variable and press "Ctrn+1", then Eclipse will suggest to create a field or local variable.

You can also use Quick Fix to create a new local / field variable for a parameter of the method. Select in the method the parameter and press Quick Fix to get the option to "Assign the parameter to new field".

You can also use Quick Fix to create a method for an object. Just type the call of the method. Eclipse will indicate that this method does not exists. Press Quick Fix to get the option to create this method.

You can also use Quick Fix to handle Exceptions, e.g. create the try / catch block for you, or add a through exception to the method.

You can also use Quick Fix to assign a statement to a new variable. Lets assume you have the statement a.calculateSomething(); whereby calculateSomething returns an integer. Select the statement and press Quick Fix to assign it to a new local / field variable.

With a Quick Fix you can also check the spelling of types words.

4.3. Important Preference Settings

Eclipse allows you to set semicolons (and other elements) automatically.
Window →  Preferences →  Java →  Editor →  Typing

Preferences

Eclipse allows you to format the source code and to organize the imports at save.
Window →  Preferences →  Java →  Editor →  Save Actions

Preferences

4.4. Task Management

Using // TODO in the coding indicates a task for eclipse.
When you tag a comment in Java source code with "TODO" the Java compiler automatically creates a corresponding task as a reminder. Opening the task navigates you back to the "TODO" in the code. Use the  Java > Compiler > Task Tags preference page to configure any other special tags (like "FIXME") that you'd like to track in the task list.

4.5. Working Sets

A common problem in Eclipse is that your data in your workspace grows and therefore your workspace is not well structured anymore. You can use working sets to organize your displayed projects / data.


Working sets group elements for display in views or for operations on a set of elements.

The navigation views use working sets to restrict the set of resources that are displayed. If a working set is selected in the navigator, only resources, children of resources, and parents of resources contained in the working set are shown. The problems view, tasks view and bookmarks view can all be filtered based on a working set via the Configure Contents view menu item. When using the search facility, you can also use working sets to restrict the set of elements that are searched.

Different views provide different ways to specify a working setOn the package explorer select the arrow down menu on the right upper corner (menu).  Select Select Working Set. 

Views that support working sets typically use the following working set selection dialog to manage existing working sets and to create new working sets:

Select Working Set

When you create a new working set you can choose from different types of working sets. In the example below you can create a resource working set, a Java working set or a help working set.

Java Working Set

If you create a new resource working set you will be able to select the working set resources as shown below. The same wizard is used to edit an existing working set. Different types of working sets provide different kinds of working set editing wizards.

Working Set ProjectS

Working sets may also be a part of a manual build workflow. With autobuild disabled the Project > Build Working Set menu becomes enabled. From here you are able to selectively build working sets of your choosing.

Note: Newly created resources are not automatically included in the active working set. They are implicitly included in a working set if they are children of an existing working set element. If you want to include other resources after you have created them you have to explicitly add them to the working set.


4.6. Synchronize package explorer with code display

On the package explorer select the arrow down menu on the right upper corner (menu). Here you find the display settings. Select the last entry "Link with Editor". This is also available on the menu via the double arrow buttons Doubel Arrows

With this selection the package explorer will always show the source file you selected in the editor. Example: if you are working on foo.java and you change in the editor to bar.java then the display in the package explorer will change.

4.7. Code Templates

If you have to type frequently the same code / part of the document you can maintain templates which can be activate via autocomplete (Ctrl + Space).

For example lets assume you are using Eclipse to edit XML files and have to write the following text frequently only with a different file name.


<para>
<programlisting>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
parse="text" href="../examples/java/wizard/MyPageTwo.java" />
</programlisting>
</para>

To create a template for this select the menu
Window → Preferences and Open Web and XML  →  XML Files → Templates

Preferences

Preferences

Press New. Create the following template. ${cursor} indicates that the cursor should be placed at this position after applying the template.

Edit Template

Now every time you type the keyword and press Ctrl+Space the system will propose adding the additional text.

Edit Template

Edit Template

4.8. Identify unused classes / methods

To check for reference on a class and / or its public methods, select the class, press the right mouse button and select
References  →  Workspace. Eclipse will then check if and how the class and its public methods are used.

References

5. Shortcuts

For all shortcut keys go to: Window →  Preferences →  Keys

5.1. Java Coding

Table 1. Important shortcuts

Shortcut Description
Ctrl + 3 Type shortcut for the command, e.g. njc to create a new Java class or npip to create a new Plug-in project.
Ctrl + S Save
Alt + Shift + X - J Run as Java application
Shift + F2 Call the Javadoc for the selected type / class / method
Ctrl + Shift + F Format source code
Ctrl + Shift + O Organize the imports / Will import the missing imports.
Alt + Shift + R Refactor / Rename method / variable
Alt + Left / Alt + right Go back to last position/ Go to next position

Alt + Strg + 0

Alt + Strg + 7

} (Easier then AltGr + 0)

{ (Easier then AltGr + 7)


5.2. Editor

Table 2. Important Shortcuts

Shortcut Description
Ctrl + Alt + Down Copy line
Ctrl + D Deletes line
Pos1 Line Start
End Line End
Ctrl + Q Last edited position
Ctrl + M Full screen mode for java coding and back
Ctrl + Shift + F
Format
Ctrl + I
Indent line

Table 3. Search

Shortcut Description
Alt+Left Alt-Right Go to prev/ next editor position in history
Ctrl + Mouseclick on a variable Go to declaration of this variable
Ctrl + . Go to previous / next problem
F3 on a variable Goto Declaration of this variable
F4 on a variable Show type hierarchy
 Ctrl +K Find next
Ctrl + J Incremental search
Ctrl + Shift + K
Find previous
Ctrl + Shift + J
Incremental find reverse
Ctrl + F
Find and Replace
Ctrl + Shift + P
Go to matching bracket

Table 4. Selecting

Shortcut Description
Ctrl + Shift + Left Select previous word
Ctrl + Shift + Right Select next word
Alt + Shift + Left Select previous element
Alt + Shift + Right Select previous element
Ctrl + Entf Delete next element
Ctrl + Left Move one element to the left
Ctrl + Right Move one element to the right

For additional shortcut keys go to: Window →  Preferences →  Keys

5.3. Programming

Table 5. Important shortcuts

Shortcut Description
Ctrl F11 Run last launched
F12 Focus to editor window
Ctrl + Shift + P Go to matching bracket
Alt + Shift + Z Surround block with try and catch

Table 6. Debugging

Shortcut Description
Cntl + Shift + B Toggle breakpoint
F11 Debug last run
F5 Single Step (Down)
F6 Single Step (Jump)
F7 Up

6. Using jars (libraries)

6.1. Adding an external library (.jar ) to the class path

To add external jars to your project perform the following:

  • Create a new folder called lib or use your existing folder

  • Select import  →  file system  →  import the .jar

  • Select your project, right mouse click and select properties. Under libraries select "Add JARs" and under “Order and Export" include your jar file and move it up to avoid conflicts.

    The following example shows how the result would look like if junit-4.4.jar would be added to a project.

    Properties for JUnit Test

6.2. Add source code for external jars

To browse the source of a type contained in library you can attach a source archive or source folder to this library. The editor will then show the source instead of a the decompiled code. Setting the source attachment also allows source level stepping with the debugger.

The Source Attachment dialog can be reached via:

Open the Java Build Path page of a project (Projects > Properties > Java Build Path). On the Libraries page expand the library's node and select the Source attachment attribute and press Edit

Maintain the location to the source attachement.

Properties for Documentation

In the Location path field, enter the path of an archive or a folder containing the source.

6.3. Add the API for a jar

Download the API of the jar and put it somewhere in your filesystem.

Open the Java Build Path page of a project (Projects > Properties > Java Build Path). On the Libraries page expand the library's node and select the Javadoc location attribute and press Edit

Maintain the location to the api.

Properities for Documentation

7. Debug

Debug your application by selecting Run  →  Debug. To set breakpoints right click in the small left column and select toggle breakpoint. After doing this you can select the properties of the breakpoint as a condition to restrict when the breakpoint should get toggeled. Here you can restrict that the breakpoint should only be executed the 12th hit (Hit Count) or you can put in a conditional expression (which you can also use for logging if you want).

8. Links and Literature

8.1. Source Code Examples

Look for examples here.

8.2. Other Eclipse resources

Name →  Web Address
Eclipse.org Homepage →  http://www.eclipse.org

→ 

→ 

→ 

→ 

→ 

→ 

→ 

→ 

→ 

→ 

→ 

→ 





-->









Contact Web Page Owner at lilliec@uncp.edu