Newsletter:

(Tutorial)Introduction To Eclipse 3.3 (Europa) - Tutorial

Introduction to Eclipse 3.3 (Europa) - Tutorial

Abstract

Eclipse Europa is a powerful, extensible IDE for building general purpose applications. One of the main applications of Eclipse is using it as a Java IDE (Integrated Development Environment).

This article will describe the usage of Eclipse as a Java IDE. It contains a short introduction of the usage of Eclipse, discuss features of Eclipse which should increase the programmers productivity, lists the most important keyboard shortcuts and gives a few tips and tricks.

Table of Contents

1. Getting started

1.1. Installation
1.2. Start Eclipse
1.3. Update Manager
1.4. Settings for Eclipse memory consumption

2. Eclipse Overview

2.1. Workspace
2.2. Eclipses User interface Elements
2.3. Perspective
2.4. View
2.5. Editor
2.6. Editor / View comparison

3. Create your first Java program in Eclipse

3.1. "Hello Eclipse world!" console program

4. Way of Working

4.1. Powerful Time Savers
4.2. Important Preference Settings
4.3. Task Management
4.4. Working Sets

5. Shortcuts

5.1. Java Coding
5.2. Editor
5.3. Programming

6. Helpful Settings

6.1. Synchronize package explorer with code display
6.2. Setting code and comments line widths / line length and text wrapping
6.3. Local History
6.4. Code Templates
6.5. Identify unused classes / methods

7. Addings jars to the build path


8. Javadoc

8.1. Using Javadoc in Eclipse
8.2. Creating Javadoc with Eclipse

9. Add source and the api description to Eclipse

9.1. Add source code for external jars
9.2. Add the API for a jar

10. Useful plug-ins for Eclipse

10.1. Overview
10.2. Eclipse Core Tools
10.3. Findbugs

11. Links and Literature

11.1. Other Eclipse resources

1. Getting started
1.1. Installation

Download Eclipse from the website http://www.eclipse.org/ and unpack it to any directory. No installation procedure is required.

Eclipse requires an installed Java as of version 1.5 It is recommended to use Java 6 (also known as Java 1.6).
1.2. Start Eclipse

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

Close the welcome page by press in little x besides the Welcome.
1.3. Update Manager

Eclipse allows to add functionality via plug-ins. You can use the update manager of eclipse to install new plug-ins. You find the update manager via the menu Help -> Software Updates -> Find and Install.

Select then "Search for new features to install".

The standard Eclipse plug-ins can be found under the "Europa Discovery Site". Select it and press finish.

After a while (you may have to select a mirror) a menu is displayed in which you can select the additional features which you would like to install.
1.4. Settings for Eclipse memory consumption

If Eclipse fails from time to time with a memory error try to extend the available memory via the file eclipse.ini in your installation file. Here are my settings.
-Xms356m
-Xmx512m
-Xmn128m
-XX:+UseParallelGC
-XX:MaxPermSize=128
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). You can take parts of your settings which you did in your workspace to the next workspace.

In your workspace all your sources files, images and other artefacts will be stored and saved.

Note: 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 workscape to c:\temp. Please note that you have to put the path name into brackets. For example:

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


2.2. Eclipses User interface Elements

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


2.3. 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 it original state use the menu "Window" -> "Reset Perspective" to reset the perspective.

Eclipse allow you to switch to another perspective via the menu Window->Open Perspective -> Other.

2.4. 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.5. Editor

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


2.6. 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 make 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 in Eclipse


3.1. "Hello Eclipse world!" console program

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

Select from the menu File -> New-> Java project. Maintain "MyFirstProject" has the project name. Select “Create separate source and output folders.

Press finish.

Open the folder, select the folder src, right mouse click on it and select create package. Create package mypackage.

Right click on your package and select New -> Class

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

Maintain the following code.

package mypackage;

public class MyFirstClass {

/**

* @param args

*/

public static void main(String[] args) {

System.out.println("Hello Eclipse World!");

}

}

Now run your code.

Finished! You should see the output in the console.

4. Way of Working

Eclipse provides tremendous help during programming. Even though it is not possible to describe all then following tried to highlight a few nice features.

The killer helpers are


*Content Help
*Quick Fix

4.1. Powerful Time Savers

Table 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.

Or type in the coding Ex and then press [Ctrl + Space] to get a list of all types which starts with Ex.

Or if you have type variable test. and then press [Ctrl + Space] you get all the possible methods.

Allows also to define variables 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 the press [Ctrl+Space] to define the name automatically. By default it will use the name of the type. If you want to add a prefix / suffix you can do this under Windows-Preferences and here Java -> Code Style.
Generated Getters and setters To create getter and setter methods for a field, select the field's declaration and invoke Source > Generate Getter and Setter.
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.2. Important Preference Settings

Eclipse allows to set semicolons (and other elements) automatically.

Eclipse let you also configure the errors / warnings it displays.

Eclipse allows to format the source code and to organize the imports at save.

4.3. Task Management

If you use // TODO in the coding this indicates a task for eclipse and you find it in the task view of Eclipse.
4.4. 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. To setup your working set select in the Package Explorer -> Show -> Working Sets.

Press new on the following dialog to create a working set.

On the following dialog select java, select the source folder you would like to see and give it a name. You can now easily display only the files you want to see.

5. Shortcuts


5.1. Java Coding
Table 2.
Important shortcuts
Shortcut Description
Ctrl + S Save
Alt + Shift + X - J Run as Java application
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 to reach then AltGr + 0)

{ (Easiwer to reach then AltGr + 7)

5.2. Editor

Table 3. 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

Table 4. Search
Shortcut Description
Alt+Left Alt-Right Go to prev/ next editor position in history
Ctrl + Mausclick on a variable Go to declaration of this variable
Ctrl + . Go to previous / next problem
F3 on a variable Goto Declaration of this variable
Strg + J , Strg +k Incremental search, find next

Table 5. 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

5.3. Programming

Table 6. 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 7. Debugging
Shortcut Description
Cntl + Shift + B Toggle breakpoint
F11 Debug last run
F5 Single Step (Down)
F6 Single Step (Jump)
F7 Up

6. Helpful Settings


6.1. 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 working on foo.java and you change in the editor to bar.java then the display in the package explorer will change.
6.2. Setting code and comments line widths / line length and text wrapping

Code line wrapping is set in Preferences->Java->Code Style- >Formatter, then click on the Edit button and select the Line Wrapping tab.

Comment width and line wrapping is set in Preferences->Java->Code Style->Formatter, then click on the Edit button and select the Comments tab.

Indentation is set separately, in Preferences->Java->Code Style- >Formatter, then click on the Edit button and select the Indentation tab.
6.3. Local History

Eclipse stores for each file a local history which you can use to retrieve an older version of the file even without using a version control system.

Double-click on on Revision Time in the history view to see the differences.

6.4. 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

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 proposal to add the additional text.

6.5. 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.

Currently it is only possible to do this class by class. The Eclipse Tools project (update site: http://eclipse.org/eclipse/platform-core/updates has functionality to check several project. Install the plug-in and select one or more Java projects, packages, or types in the Package Explorer, and invoke "Find Unreferenced Members" from the context menu. For most Eclipse projects, selecting all non-API packages is a good start, although it is possible for API packages to contain unused package-private members, or protected members in final classes that can be deleted.
7. Addings jars to the build 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.

8. Javadoc


8.1. Using Javadoc in Eclipse

To call the Javadoc for a type / class / method, position the cursor on the keyword and press Shift + F2.

8.2. Creating Javadoc with Eclipse

To create Javadoc from Eclipse for your coding, select File -> Export. Select then Javadoc and follow the wizard.

The javadoc command is the javadoc.exe file which you will find in your jdk installation directory.

9. Add source and the api description to Eclipse


9.1. 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.

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

10. Useful plug-ins for Eclipse


10.1. Overview

The following lists useful plug-ins for Eclipse which have not yet become mainstrain. So I'm not listening here plug-ins like WTP but other not so known plug-ins which I particular find useful.
10.2. Eclipse Core Tools

The Eclipse Core Tools provide functionality for checking several classes / packages for unused methods.

The webpage seem not to be active but the tool seems to be still developed.

http://www.eclipse.org/eclipse/platform-core/main.html Eclipse Core Tools

http://eclipse.org/eclipse/platform-core/updates Update Site


10.3. Findbugs

Findbugs offers static code analysis to identify bugs in your source code.

http://findbugs.sourceforge.net Eclipse Core Tools

http://findbugs.cs.umd.edu/eclipse Update Site

After installing findbugs you can activate findbugs for your project via the properties of your project and select FindBugs -> Run FindBugs automatically.

11. Links and Literature


11.1. Other Eclipse resources

http://www.eclipse.org Eclipse.org Homepage

http://www.vogella.de Articles about Java and Eclipse

http://www.vogella.de/articles/Eclipse/article.html Using Eclipse as IDE - Tutorial

http://www.vogella.de/articles/RichClientPlatform/article.html Eclipse Rich Client Platform Tutorial - A Hands-on-Guide

http://www.vogella.de/articles/EclipseMicrosoftIntegration/article.html Eclipse Microsoft Integration - Tutorial

http://www.vogella.de/articles/EclipseCodeAccess/article.html A guide to access the Eclipse Sources

http://www.vogella.de/articles/EclipseJFreeChart/article.html Using JFreeChart with an Eclipse RCP application.

http://www.vogella.de/articles/EclipseJFaceTable/article.html Using the JFace 3.3 API to create a table in an Eclipse RCP application.

http://www.vogella.de/articles/EclipseDali/article.html Use Eclipse Dali for working with databases

http://www.vogella.de/articles/EclipseTPTP/article.html Tutorial for testing Web applications with the Eclipse Test and Performance Tools Platform (TPTP)

http://www.vogella.de/articles/EclipseWTP/article.html Using Eclipse WTP to build servlets, manage database access for webapplication and to handle webservices.

http://www.vogella.de/articles/EclipseEMF/article.html Building a HTML Website with the Eclipse Modeling Framework (EMF) and Java Emitter Template (JET) - Tutorial
Courtesy: vogella.de