![]() |
|
Eclipse Tutorial | Fall 2009 |
![]() |
||||||||
|
||||||||||||
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. InstallationDownload 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 EclipseTo 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. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Select a suitable folder and press Ok. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Eclipse will start and show the Welcome page. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
![]()
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. WorkspaceThe 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 timeTo 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 ElementsEclipse provides perspectives, views and editors. Views and editors are grouped into perspectives. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2.4. PerspectiveA 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.)
TipIf 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. ViewViews 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. EditorEditors are meant for for the primary focus of attention and shows the main content of your application. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2.7. Editor / View comparisonThe 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:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
3. Create your first Java program |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
3.1. "Hello Eclipse Ganymede!" console programThe 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.
![]() Press finish to create the project. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
A new project is created and displayed as a folder.
![]() Open the folder "FirstGanymede", select the folder src, right mouse click on it and select create package. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Create package "mypackage".
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Type the package name in Name: box.
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Right click on your package and select New → Class ![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Create MyFirstClass, select the flag "public static void main (String[] args)"
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Enter the following code.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
4. Productive Usage of Eclipse |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
4.1. Content assistThe 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 FixWhenever 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.
![]() Eclipse allows you to format the source code and to organize
the
imports at save.
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
4.4. Task Management Using // TODO in the coding indicates a task for
eclipse. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 set. On
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:
![]() 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.
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.
S
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 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 TemplatesIf 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.
![]()
![]() Press New. Create the following template. ${cursor} indicates that the cursor should be placed at this position after applying the template.
![]() Now every time you type the keyword and press Ctrl+Space the system will propose adding the additional text.
![]()
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
5. ShortcutsFor all shortcut keys go to: Window → Preferences → Keys |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
5.1. Java
Coding
|
| 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) |
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 |
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 |
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.

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.

In the Location path field, enter the path of an archive or a folder containing the source.
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.

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).
| Name | → | Web Address |
|---|---|---|
| Eclipse.org Homepage | → | http://www.eclipse.org |
| → | ||
| → | ||
| → | ||
| → | ||
| → | ||
| → | ||
| → | ||
| → | ||
| → | ||
| → | ||
| → | ||
| → | ||
|
|
|
|
| Contact Web Page Owner at lilliec@uncp.edu |