Eclipse Tutorial
Eclipse Tutorial
Audience
This tutorial has been prepared for beginners to help them understand basic functionality
of Eclipse tool. After completing this tutorial, you will find yourself at a moderate level of
expertise in using Eclipse IDE from where you can take yourself to next levels.
Prerequisites
We assume you are going to use Eclipse IDE to handle all levels of Java projects
development. So it will be good if you have knowledge of software development using any
programming language specially Java programming.
Eclipse
Table of Contents
About the Tutorial .................................................................................................................................... i
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
Copyright & Disclaimer............................................................................................................................. i
Table of Contents .................................................................................................................................... ii
1.
OVERVIEW........................................................................................................................... 1
What is Eclipse? ...................................................................................................................................... 1
Licensing ................................................................................................................................................. 1
Eclipse Releases ...................................................................................................................................... 1
2.
INSTALLATION ..................................................................................................................... 3
Downloading Eclipse ............................................................................................................................... 3
Installing Eclipse ...................................................................................................................................... 3
Launching Eclipse .................................................................................................................................... 4
3.
4.
5.
Eclipse
6.
PERSPECTIVES ................................................................................................................... 13
What is a Perspective? .......................................................................................................................... 13
Opening a Perspective ........................................................................................................................... 13
Switching between Perspectives ........................................................................................................... 13
Closing a Perspective ............................................................................................................................. 14
Customizing a Perspective ..................................................................................................................... 14
7.
WORKSPACES .................................................................................................................... 16
About Eclipse Workspace ...................................................................................................................... 16
UI Elements for Managing the Workspace ............................................................................................ 16
8.
9.
Eclipse
Eclipse
27. NAVIGATION...................................................................................................................... 58
Navigating the Eclipse Workspace ......................................................................................................... 58
Open Type ............................................................................................................................................. 58
Open Type in Hierarchy ......................................................................................................................... 60
Open Resource ...................................................................................................................................... 61
Eclipse
vi
1. OVERVIEW
Eclipse
What is Eclipse?
In the context of computing, Eclipse is an integrated development environment (IDE) for
developing applications using the Java programming language and other programming
languages such as C/C++, Python, PERL, Ruby etc.
The Eclipse platform which provides the foundation for the Eclipse IDE is composed of
plug-ins and is designed to be extensible using additional plug-ins. Developed using Java,
the Eclipse platform can be used to develop rich client applications, integrated
development environments, and other tools. Eclipse can be used as an IDE for any
programming language for which a plug-in is available.
The Java Development Tools (JDT) project provides a plug-in that allows Eclipse to be used
as a Java IDE, PyDev is a plugin that allows Eclipse to be used as a Python IDE, C/C++
Development Tools (CDT) is a plug-in that allows Eclipse to be used for developing
application using C/C++, the Eclipse Scala plug-in allows Eclipse to be used an IDE to
develop Scala applications and PHPeclipse is a plug-in to eclipse that provides complete
development tool for PHP.
Licensing
Eclipse platform and other plug-ins from the Eclipse foundation is released under the
Eclipse Public License (EPL). EPL ensures that Eclipse is free to download and install. It
also allows Eclipse to be modified and distributed.
Eclipse Releases
Every year, since 2006, the Eclipse foundation releases the Eclipse Platform and a number
of other plug-ins in June.
Codename
Year
Platform Version
Callisto
2006
3.2
Europa
2007
3.3
Ganymede
2008
3.4
Galileo
2009
3.5
Helios
2010
3.6
Eclipse
Indigo
2011
3.7
Juno
2012
Kepler
2013
4.3
Luna
2014
4.4.0
2. INSTALLATION
Eclipse
Downloading Eclipse
You can download eclipse from http://www.eclipse.org/downloads/. The download page
lists a number of flavors of eclipse.
The capabilities of each packaging of eclipse are different. Java developers typically use
Eclipse Classic or Eclipse IDE for developing Java applications.
The drop down box in the right corner of the download page allows you to set the operating
system on which eclipse is to be installed. You can choose between Windows, Linux and
Mac. Eclipse is packaged as a zip file.
Installing Eclipse
To install on windows, you need a tool that can extract the contents of a zip file. For
example you can use:
7-zip
PeaZip
IZArc
Using any one of these tools, extract the contents of the eclipse zip file to any folder of
your choice.
Eclipse
Launching Eclipse
On the windows platform, if you extracted the contents of the zip file to c:\, then you can
start eclipse by using c:\eclipse\eclipse.exe
When eclipse starts up for the first time it prompts you for the location of the workspace
folder. All your data will be stored in the workspace folder. You can accept the default or
choose a new location.
If you select "Use this as the default and do not ask again", this dialog box will not come
up again. You can change this preference using the Workspaces Preference Page. See the
Preference tutorial page for more details.
4
3. EXPLORE WINDOWS
Eclipse
Views
Menu Bar
Toolbar
An eclipse perspective is the name given to an initial collection and arrangement of views
and an editor area. The default perspective is called java. An eclipse window can have
multiple perspectives open in it but only one perspective can be active at any point of
time. A user can switch between open perspectives or open a new perspective. A
perspective controls what appears in some menus and tool bars.
A perspective has only one editor area in which multiple editors can be open. The editor
area is usually surrounded by multiple views. In general, editors are used to edit the
5
Eclipse
project data and views are used to view the project metadata. For example, the package
explorer shows the java files in the project and the java editor is used to edit a java file.
The eclipse window can contain multiple editors and views but only one of them is active
at any given point of time. The title bar of the active editor or view looks different from all
the others.
The UI elements on the menu bar and tool bar represent commands that can be triggered
by an end user.
4. EXPLORE MENUS
Eclipse
File menu
Edit menu
Navigate menu
Search menu
Project menu
Run menu
Window menu
Help menu
Plug-ins can add new menus and menu items. For example when the java editor is open,
you will see the Source menu and when the XML editor is open, you will see the Design
menu.
7
Eclipse
Menu
Name
File
The File menu allows you to open files for editing, close editors, save editor
content and rename files. Among the other things, it also allows you to
import and export workspace content and shutdown Eclipse.
Edit
Source
The Source menu is visible only when a java editor is open. It presents a
number of useful menu items related to editing java source code.
Navigate
The Navigate menu allows you to quickly locate resources and navigate to
them.
Search
The Search menu presents items that allow you to search the workspace
for files that contain specific data.
Project
The menu items related to building a project can be found on the Project
menu.
Run
The menu items on the Run menu allow you to start a program in the run
mode or debug mode. It also presents menu items that allow you to debug
the code.
Window
The Window menu allows you to open and close views and perspectives. It
also allows you to bring up the Preferences dialog.
Help
The Help menu can be used to bring up the Help window, Eclipse
Marketplace view or Install new plug-ins. The about Eclipse menu item gives
you version information.
Customizing Menus
The visible menu items on a menu depend on the installed plug-ins and customization
done using the Customize Perspective dialog box.
5. EXPLORE VIEWS
Eclipse
About Views
Eclipse views allow users to see a graphical representation of project metadata. For
example the project navigator view presents a graphical representation of the folders and
files associated with a project and properties view presents a graphical representation of
an element selected in another view or editor.
An eclipse perspective can show any number of views and editors. All editor instances
appear in a single editor area, whereas views are placed inside view folders. A workbench
window can display any number of view folders. Each view folder can display one or more
views.
Organizing Views
The following picture shows four views arranged in a view folder.
The picture given below shows the same four views arranged in two view folders.
Moving Views
To move a view from one view folder to another, just click on the view title and drag to
the title bar area of another view folder. The green line shown below is a result of dragging
the title bar of the Properties view from one view folder to the title bar area of another
9
Eclipse
view folder. The Properties view can be moved to where the green line is by releasing the
mouse button and sending out a drop event.
Moving the drag icon to the bottom of a window allows you to create a view folder that
spans the entire width of the window. Moving the drag icon to the left or right edge of
window allows you to create a view folder that spans the entire height of the window.
Opening a view
To open a view, click on the Window menu and select the Show View menu item.
10
Eclipse
Clicking on the Other menu item brings up the Show View dialog box that allows you to
locate and activate a view.
11
Eclipse
The views are organized by category. To quickly locate a view, just type the name of a
view into the filter text box. To open a view, select it and click on the OK button. The
subsequent pages of this tutorial introduce you to a number of useful views.
12
6. PERSPECTIVES
Eclipse
What is a Perspective?
An eclipse perspective is the name given to an initial collection and arrangement of views
and an editor area. The default perspective is called java. An eclipse window can have
multiple perspectives open in it but only one perspective is active at any point of time. A
user can switch between open perspectives or open a new perspective. The active
perspective controls what appears in some menus and tool bars.
Opening a Perspective
To open a new perspective, click on the Windows menu and select Open Perspective
Other
The Open Perspective dialog box shows all the available perspectives. The same dialog can
be brought up by clicking on the Open Perspective button (
Eclipse
Closing a Perspective
To close a perspective, right click on the perspective name in toolbar and select the Close
menu item.
Customizing a Perspective
The customize perspective dialog can be used to customize a perspective. Customizing a
perspective means:
Determine the menu items in New submenu, Show View submenu and Open
Perspective submenu
Eclipse
The Tool Bar Visibility tab can be used to determine which icons are visible on
the toolbar when a perspective is open.
The Menu Visibility tab can be used to determine which menu items are visible
when a perspective is active.
The Command Groups Availability tab can be used to control the visibility of
toolbar icons and menu items.
The Shortcuts tab can be used to determine the menu items in New submenu,
Show View submenu, and Open Perspective submenu.
Based on the Shortcuts selection in the picture given below, to bring up the "New Java
Project from Existing Ant Build File" wizard users have to bring up the New submenu (File
New), click on Other, expand the Java category and then select "Java Project from
Existing Ant Build File". On the other hand, to start the New Class wizard they can bring
up the New submenu (File New) and select the Class menu item because its selected in
the picture given below. By selecting the "Java Project from Existing Ant Build File" check
box this item will also appear under the New menu.
15
7. WORKSPACES
Eclipse
Projects
Files
Folders
The workspace has a hierarchical structure. Projects are at the top level of the hierarchy
and inside them you can have files and folders. Plug-ins use an API provided by the
resources plug-in to manage the resources in the workspace.
The File Wizard (File New File) can be used to create a new file.
16
Eclipse
The Folder Wizard (File New Folder) can be used to create a new folder.
17
Eclipse
By right clicking anywhere in the Project Explorer and selecting New Java Project
Select the Project Layout which determines whether there would be a separate
folder for the source codes and class files. The recommended option is to create
separate folders for sources and class files.
18
Eclipse
You can click on the Finish button to create the project or click on the Next button to
change the java build settings.
On the second page you can change the Java Build Settings like setting the Project
dependency (if there are multiple projects) and adding additional jar files to the build path.
19
Eclipse
20
Eclipse
If you are creating a sub package, before opening the Java Package wizard select the
parent package so that name field can have a default value in it.
21
Eclipse
22
Eclipse
Before bringing up the New Java Class wizard, if possible, select the package in which the
class is to be created so that the wizard can automatically fill in the package name for you.
Enter the super class name or click on the Browse button to search for an existing
class
Click on the Add button to select the interfaces implemented by this class
Examine and modify the check boxes related to method stubs and comments
23
Eclipse
24
Eclipse
Before bringing up the New Java Interface wizard, if possible, select the package in which
the interface is to be created so that the wizard can automatically fill in the package name
for you.
Select the Generate comments check box if you like comments to be generated
25
Eclipse
26
Eclipse
Press ctrl + N
) select other or
In the filter text box enter xml, this should show only the wizards related to xml
Note
If the XML perspective is open the New XML wizard can be invoked by:
27
Eclipse
Click on the Next button to base the xml file on DTD, XML Schema or XML template
else click on Finish
28
Eclipse
The XML editor allows you to edit an XML file using either the Design view or Source view.
29
Eclipse
The java build path can be seen and modified by using the Java Build Path page of the
Java Project properties dialog.
To bring up the Java Project properties dialog box, right click on a Java Project in the
Package Explorer view and select the Properties menu item. On the left hand side tree
select Java Build Path.
A common requirement seen while developing java applications is to add existing jars to
the java build path. This can be accomplished using the Libraries tab. In the Libraries tab,
just click on Add JARs if the jar is already in the Eclipse workspace or click on Add External
JARs if the jar is elsewhere in the file system.
30
Eclipse
To create a run configuration for a Java application select "Java Application" from the list
on the left hand side and click on the New button. In the dialog box that comes up in the
main tab specify:
31
Eclipse
The Commons tab provides common options such as the ability to allocate a console for
standard input and output.
To save the run configuration click on the Apply button and to launch the application click
on the Run button.
32
Eclipse
Right click on the java class that contains the main method
The same action can be performed using the Package Explorer view by selecting the class
that contains the main method and clicking Alt + Shift + X, J
Either actions mentioned above create a new Run Configuration and use it to start the
Java application.
If a Run configuration has already been created you can use it to start the Java application
by selecting Run Configurations from the Run menu, clicking on the name of the run
configuration and then clicking on the Run button.
33
Eclipse
The Run item on the Run menu can be used to restart the java application that was
previously started.
The shortcut key to launch the previously launched Java application is Ctrl + F11.
34
Eclipse
In the Package Explorer select the items that you want to export. If you want to
export all the classes and resources in the project just select the project.
In the filter text box of the first page of the export wizard type in "JAR".
Click on Next
The default is to export only the classes. To export the source code also, click on
the "Export Java source files and resources" check box.
35
Eclipse
Click on Finish
36
Eclipse
Consume memory
Take up build time especially when the Clean All Projects (Project Clean all
projects) with the Start a build immediately option is used.
37
Eclipse
38
Eclipse
Once the project is open its content can be edited using the Eclipse user interface.
39
Eclipse
In the Package Explorer view right click on the project and select Properties
It's the java builder that distinguishes a Java project from other types of projects. By click
on the New button you can associate the Ant builder with a java project. The java builder
is responsible for compiling the java source code and generating classes.
The java builder is notified of changes to the resources in a workspace and can
automatically compile java code. To disable automatic compilation deselect the Build
Automatically option from the Project menu.
40
Eclipse
If automatic compilation is disabled then you can explicitly build a project by selecting the
Build Project menu item on the Project menu. The Build Project menu item is disabled if
the Build Automatically menu item is selected.
41
Eclipse
To create a debug configuration for a Java application, select "Java Application" from the
list on the left hand side and click on the New button. In the dialog box that comes up in
the main tab specify:
42
Eclipse
To save the run configuration, click on the Apply button and to launch the application in
the debug mode click on the Debug button.
43
Eclipse
Right click on the java class that contains the main method
The same action can be performed using the Package Explorer by selecting the class that
contains the main method and clicking Alt + Shift + D, J
Either actions mentioned above create a new Debug Configuration and use it to start the
Java application.
If a Debug configuration has already been created you can use it to start the Java
application by selecting Debug Configurations from the Run menu, clicking on the name
of the debug configuration and then clicking on the Debug button.
The Debug menu item on the Run menu can be used to restart the java application that
was previously started in the debug mode.
44
Eclipse
The shortcut key to launch the previously launched Java application in the debug mode is
F11. When a java program is started in the debug mode, users are prompted to switch to
the debug perspective. The debug perspective offers additional views that can be used to
troubleshoot an application.
The java editor allows users to place break points in the java code. To set a break point,
in the editor area right click on the marker bar and select Toggle Breakpoint.
Breakpoints are shown on the marker bar. They are also visible in the Breakpoints View.
45
Eclipse
When the execution of code reaches a break point the JVM suspends the execution of the
program. At this point, you can inspect the memory and execute the program in a
controlled manner.
When the execution of a program is paused, the debug view can be used to inspect the
call stack.
The run menu has menu items that allow you to Resume execution, step over a line of
code, step into a function etc.
46
Eclipse
The picture given above shows the shortcut keys associated with actions such as Resume,
Step Into and Step Over, etc.
47
22. PREFERENCES
Eclipse
Setting Preferences
The Preferences dialog allows Eclipse users to manage their preferences. This dialog box
is managed by the framework but any plug-in can add multiple pages to the dialog box.
To invoke this dialog, click on the Window menu and select the Preferences menu item.
The preference pages are organized by category. You can locate a preference page by
expanding the nodes of the tree in the left hand side of the dialog box.
The filter text box on the left hand side top corner allows you to quickly locate a preference
page. As you type characters into the filter text box the items shown in the left hand side
tree are filtered. For e.g. type in font to quickly get to the Font preferences page.
48
Eclipse
After you make changes to the appropriate preferences click on the OK button to save the
changes and close the dialog box or click on the Cancel button to discard the changes and
close the dialog box.
49
Eclipse
50
Eclipse
To select an item from the list just highlight the item using the up or down arrow keys and
press enter.
If you type some characters and then press Ctrl + space the dialog box will show only
items that start with the characters typed in.
51
Eclipse
Adds a light bulb with a warning sign or error sign to the vertical ruler
The quick fix dialog provides a list of possible corrections. The quick fix dialog can be
invoked by:
Placing the cursor in the highlighted text and selecting Quick fix from the Edit menu
or by clicking shortcut Ctrl + 1
52
Eclipse
In the above diagram, getId is highlighted because the Person class does not have a
method called getId(). Selecting "Create method 'getId()' in type 'Person'" would cause
the getId() method to be added to the Person class.
The Quick Fix dialog can also be displayed by right clicking on the error item in the
Problems view and selecting the Quick Fix menu item.
53
Eclipse
In the java editor, placing the mouse pointer over the class being imported displays the
java doc associated with that class.
54
Eclipse
Placing a mouse pointer on a method shows the java documentation for that method.
55
Eclipse
Clicking Ctrl + H
The File Search page allows you to search any type of file but the Java Search page focuses
on Java files only.
56
Eclipse
For example, to search for usage of the Person type, in the Java Search page:
Click Search
57
27. NAVIGATION
Eclipse
Among them, the Open Type, Open Type in Hierarchy and Open Resource menu items
tend to be very useful.
Open Type
The Open Type menu item brings up a dialog box that allows you to locate a Java type. In
the filter text box enter in either the fully qualified name or name of a class. The '*'
character which stands for 0 or more characters and '?' which stands for a single character
can be used to specify patterns. The dialog box will show all the names that match the
given pattern.
58
Eclipse
59
Eclipse
You can use the Attach Source button to specify where the source code for the class is
located.
The source code for the types that comes with the java distribution is in src.zip which is
located in the java home folder.
60
Eclipse
Once you select a type, its hierachy is shown in the Type Hierarchy view.
The Type Hierarchy is an excellent view for learning about the hierarchy of a type. On the
left hand side pane: type, its super type and sub type are available. On the right hand side
pane: attributes and methods of a selected type are shown.
Open Resource
The open resource menu item can be used to locate a file in the workspace. The '*'
character which stands for 0 or more characters and '?' which stands for a single character
61
Eclipse
can be used to specify patterns. The dialog box will show all the names that match the
given pattern.
Select the file that you want to open in an editor and click on the OK button.
62
28. REFACTORING
Eclipse
Right clicking on a Java element in the Package Explorer view and selecting Refactor
menu item
Right clicking on a Java element in the Java editor and selecting Refactor menu
item
Selecting a Java element in either the Package Explorer view or Java Editor and
clicking Shift + Alt + T
The refactor menu shows all the possible changes that are supported on the selected
Java element. To rename a class, select the Rename menu item.
Click on the Next button to see a preview of the changes (if available). Then click on Finish
to get the wizard to do its work.
63
Eclipse
About Bookmarks
Eclipse editors allow you to place a book mark on any line within the editor. You can use
bookmarks to serve as reminders and quickly navigate to any line in a file.
Adding a Bookmark
To set a bookmark, right-click on the vertical ruler near the line on which you want the
bookmark and select "Add Bookmark". In the Add Bookmark dialog box enter a name for
the Bookmark
A bookmark icon appears in the vertical ruler. Also, a row is added to the table in the
Bookmarks view.
Eclipse
Click on OK
Using the Bookmarks view you can navigate to a bookmark by double clicking on a
bookmark or right clicking on a bookmark and selecting "Go to" menu item.
65
Eclipse
Managing Tasks
Programmers like to place TODO markers in their code which serves as a reminder for
tasks that need to be completed. Comments in the Java code that has the word TODO in
them are recognized as task and shown on the marker bar and in the Tasks view.
An eclipse editor can be used to associate tasks with the file being edited by right clicking
on the marker bar and selecting Add Task. In the dialog box that comes up enter the task
description and select a priority from the priority drop down list and then click on the OK
button.
66
Eclipse
To remove a task using an Eclipse editor, right-click on the task icon in the marker bar
and select the Remove Task menu item.
Click OK
67
Eclipse
It can also be used to:
68
Eclipse
From within the Eclipse IDE, you can search the market place by using the Eclipse
Marketplace dialog which can be invoked by clicking on the Help menu and selecting Eclipse
Marketplace.
69
Eclipse
In the screenshot above, to install PyDev - Python IDE for Eclipse, just click on the Install
button. Using the dialog box that comes up you can install the plug-in.
70
Eclipse
You can also install a plug-in by using the Install New Software menu item accessible from
the Help menu (Help Install New Software)
71
Eclipse
In this case, you have to know the URL of the remote site from which the plug-in can be
downloaded. You can supply this URL by clicking on the Add button.
The install dialog box shows the plug-in components available in the remote site and
allows you to install them.
72
Eclipse
Selecting "for - iterate over array" inserts code for iterating through depArray which was
declared earlier in the method body. Another useful code template literal is sysout which
can be used to insert the System.out.println() method call.
73
Eclipse
You can read more about the variables that can be used to customize the code that is
inserted by reading the Eclipse help. To bring up the help window click on the Help menu
and select Help Contents. In the search text box enter "Java Editor Template Variables".
From the list on the left hand side select "Java Editor Template Variables".
74
33. SHORTCUTS
Eclipse
About Shortcuts
You can navigate the Eclipse User Interface using only the keyboard by:
Using accelerators associated with actions such as menu items and buttons on the
toolbar
The Key Assist action which can be invoked by clicking Ctrl + Shift + L shows us all the
accelerators or shortcut keys available in Eclipse.
75
Eclipse
The key combination assigned to an action can be changed using the Keys preference
page. There are many commands or actions in Eclipse for which there are no shortcut
keys. This preference page also allows you to assign shortcut keys to these actions.
For example, to assign Alt + C as the shortcut key to trigger the command that brings up
the New Class dialog box select New Class in the command list, navigate to the
Bindings text box and then press Alt + C. The "when dropdown list" allows you to select
a context in which the shortcut key is valid. Ensure that there are no conflicts for the
selected key combination and click on the OK button.
76
Eclipse
Now you can press Alt + C to bring up the New Class wizard.
77
Eclipse
Restarting Eclipse
The restart option allows users to restart eclipse. To restart eclipse, click on the File menu
and select the Restart menu item.
After a plug-in is installed, users will be prompted to restart eclipse. If they choose not to
restart at that point they can restart eclipse later by using the restart option. If eclipse
user interface misbehaves, users may want to restart eclipse using this option.
78
Eclipse
Press Ctrl + Shift + L to open a widget that shows all the shortcut keys.
In the Java editor press Ctrl + Space to see a list of suggested completions. Typing
one or more characters before clicking Ctrl + Space will shorten the list.
When the cursor is in a method argument, press Ctrl + Shift + Space to see a list
of parameter hints.
Code completion supports camel case patterns. Entering NPE and clicking Ctrl +
Space will propose NullPointerException and NoPermissionException
Click on Source Generate Getter and Setter to open the wizard that allows you
to generate getter and setter methods.
Click on Source Generate hashCode() and equals() to generate this methods for
a Java class
Select a block of code and press Alt+Shift+Z to see a menu of items like if
statement, for loop, try/catch etc that can enclose the selected block of code .
Select an opening or closing bracket and press Ctrl+Shift+P to find its matching
bracket.
Type '/**' and press Enter to automatically add a Javadoc comment stub.
79
Eclipse
Press Ctrl+F7 to see a list of open views and editor area and switch to one of
them.
80
Eclipse
To navigate to a web page, in the URL text box of the Internal Web Browser, enter a URL
of required web page.
81