Está en la página 1de 14

Basic Eclipse IDE Tutorial

This tutorial is designed to help students get started with using the Eclipse IDE
By the end of this tutorial you will have been guided through all of the basic features of the Eclipse IDE
application that you will need to perform day-to-day programming tasks that may be required in introductory
java programming courses.
NOTE: This tutorial is intended for students with prior java programming experience.

Introduction: What is the Eclipse IDE?


The Eclipse Integrated Development Environment (IDE for short) is an application which ties together all of the tools
you need to organise, write, compile, debug and run java programs.
While it is targeted towards programmers working on larger projects which may involve the organisation and writing of
many classes, it can also be used as an effective programming tool by programmers learning the Java programming
language.
Features the Eclipse IDE encapsulates include:

A fully-customisable development environment which by default brings together the following features:
o

An intelligent code editor which allows multiple source code files to be opened.

Integrated source code pre-compilation.

A runtime console for displaying program output.

A package explorer which allows the developer to navigate through the structure of the projects that
have been developed.

A class outline which allows the developer to quickly navigate to a specific class unit (method or
variable) when required.

The standard Java compiler and runtime environment for all versions of java currently in use (including
version 7.0, which is the latest release)

A useful Java debugger

Other useful tools such as templates for generating javadoc (source code documentation) and skeleton code
for classes, interfaces and methods.

Students undertaking Java programming courses in RMIT CS&IT are expected to familiarise themselves with the
Eclipse IDE in order to make use of it when doing their course (assignment) work.

RMIT- School of Computer Science and Information Technology


Introduction to Eclipse Tutorial

Page 1

Section 1: Starting up the Eclipse IDE Application


You should be able to access eclipse in windows from any of the terminals in the labs on 14.09 and 14.10 - you
should be able to find it either in the start menu or by clicking on the Start button and typing in eclipse in the
search/run box that appears.
If you have difficulties starting up eclipse then ask one of your lab assistants for a helping hand.
The Eclipse IDE organises development files into a workspace (working directory) in which all source code and
configuration files are stored for the current eclipse session.
Eclipse allows developers to maintain as many different workspaces as they require, so when you run the eclipse
application you have to nominate the workspace you wish to use.
If you want to create a new workspace then you can use the browse button to locate the directory you want the new
workspace to be stored in and/or enter the name (or full path) of the workspace directory manually as shown below.
If an eclipse workspace is not present at the specified location then a new directory (or path is required) will be
created in the nominated directory.
If you are running eclipse in windows you should make sure that your workspace is created in on your own personal
Student drive - create a workspace for Programming 1 in your student drive to get started.After you have
selected/created your workspace the Eclipse IDE application will start up and you will be presented with a welcome
screen if this is the first time you have used eclipse.
To proceed to the default eclipse view you need to click on the workbench link as displayed in the Figure 1.1 below.

Figure 1.1 The welcome screen

NOTE: you can return to the welcome screen by selecting the Welcome option in the Help menu if you wish to
examine the other options in the welcome screen (including the configuration examples and tutorial links).
Once you have selected to move to the workbench you will be presented with the default workbench shown below it
will more than likely start up in a perspective for a different type of programming, so you should change the
perspective to the normal Java setting as shown below:
RMIT- School of Computer Science and Information Technology
Introduction to Eclipse Tutorial

Page 2

Figure 1.5 Changing perspective

In the next section you will be guided through the process of setting up a basic project and writing a basic program
using the default workbench.

RMIT- School of Computer Science and Information Technology


Introduction to Eclipse Tutorial

Page 3

Section 2 Writing a simple Java program in Eclipse


Task 1 - creating/selecting a project to work on:
Before you can start writing java programs in Eclipse you need to select an existing project that the new program will
become part of OR create a new project for the program you plan to write.
NOTE: It is recommended that you create a new project for each new assignment (java program) you wish to
implement in eclipse so that all files related to a specific assignment can be all be found in the once place.
For this tutorial you will need to create a new project to store the programs you will work on as part of this session you can start a new project by selecting File -> New -> Project as shown in figure 2.1 below.

Figure 2.1 Menu option for creating a new project

You can also use the new project shortcut to access the project creating facility as shown in figure 2.2 below.

Figure 2.2 New project shortcut

RMIT- School of Computer Science and Information Technology


Introduction to Eclipse Tutorial

Page 4

Once you have selected the new project option by either one of the methods shown above you will need to select a
new project wizard (a helper facility which assists developers to quickly and easily configure the project to their
requirements).
You should select the option to create a new project by using either one of the methods described above
now.
For most java applications you will write as part of coursework you are required to do in RMIT CS&IT java
programming courses you will only need to create a normal java project as shown in figure 2.3 below.

Figure 2.3 New Java Project Wizard screen

Once you have selected the normal java project wizard and clicked on the next button you will be taken to the Java
Project wizard - for simple java applications you only need to give the project a name and then click on the Finish
button as shown in figure 2.4 below.
You should create a new project called exercises in your workspace as shown above when your instructor
directs you to do so.
NOTE: If you wish to configure a new project in more detail then there are additional options to create a folder
(package) structure, link existing source code folders to the project and several other features, which you can explore
at your own leisure.
Once you have created your new project it will be added to the package explorer area on the workbench as shown
below in figure 2.5 and you will be able to add new classes (and packages if needs be) to the project.

RMIT- School of Computer Science and Information Technology


Introduction to Eclipse Tutorial

Page 5

Figure 2.4 Workbench after project has been created

Task 2 Define a new class and add it to the project


The next step in setting up your project so that it is ready for you to implement your application could be creating a
new package to group together the source code files we will need to implement, but we will skip this step for now.
When you want to define a new class in Eclipse all you need to do is select File -> New -> Class as shown in figure
2.6 below.

Figure 2.6 New class menu option

RMIT- School of Computer Science and Information Technology


Introduction to Eclipse Tutorial

Page 6

You can also use the "New Class" shortcut to start the class definition process as shown in figure 2.7 below.

Figure 2.7 New class shortcut

Once you have selected the New Class option you will be taken to the new class wizard, which allows you to
customise the new class to meet your requirements, such as including method stubs (empty methods which you can
fill in) for constructors, inherited abstract methods and the main method, specifying a package the new class is to be
part of and specifying the superclass of the new class we are creating (if it is intended to be a subclass) and selecting
various class modifiers (visibility, abstract, final, etc) if required.
All we need to do here for now is specify the class name and include a method stub for the main method as shown in
figure 2.8 below and click on the Finish button.

Figure 2.8 New class wizard

You should create a new class called HelloWorld as shown above now (you can ignore the default package
warning).
RMIT- School of Computer Science and Information Technology
Introduction to Eclipse Tutorial

Page 7

Task 3 Implement, compile and run the program:


Once you have created your new class the workbench will be updated as shown below in figure 2.9 note that the
different areas in the workbench have all become active now that we are creating (implementing a class).

Figure 2.9 Workbench updated with new project and class information

The different sections listed above serve the following purposes:

The package explorer (leftmost area) displays the classes that have been added to the current project.

The editor includes features such as syntax highlighting, automatic indentation and automatic syntax checking
which highlights errors as you type.

The class/method outline (rightmost area) allows you to navigate quickly and easily to the method/variable
that you need to change especially useful for classes that have more than a few methods.

The compiler error listing (bottom area) displays any compilation errors or warnings that were noted when you
saved your source code in tabular form.

Each of the regions in the workbench can also be resized to meet your preferences by clicking on and dragging one of
the corresponding borders to make the region in question either larger or smaller to suit your preferences.
Eclipse remembers the previous layout used for the workbench so that you do not have to adjust it again every time
you want to use Eclipse to write another program.

RMIT- School of Computer Science and Information Technology


Introduction to Eclipse Tutorial

Page 8

The next step we will take is to implement the program and examine how the syntax checking and error reporting
features work.
Remove the TODO comment by highlighting it and hitting the delete key and add the following (incorrect) statements
to the main method in the editor window:

System.println("Hello World")
System.println("My number is : " + number);
You should add these statements to the main method now.
Save and note how the syntax checker has highlighted the errors in a manner similar to what is shown below in figure
2.10 below (both lines have basic syntax errors).

Figure 2.10 Errors flagged by syntax checker

To see a description of the problems the syntax checker has detected simply run the mouse pointer over the
error flag as shown in figure 2.11 below.

RMIT- School of Computer Science and Information Technology


Introduction to Eclipse Tutorial

Page 9

Figure 2.11 Descriptions of errors flagged by syntax checker

You should proceed to rectify the errors as discussed below while your instructor describes how
errors are reported and can be located in this next section.
To rectify the errors on this on the first line you need to change the statement as shown below (in the
underlined text):

System.out.println("Hello World");
Now lets look at the error listing feature - save the project by clicking on the save (floppy disk) shortcut as
shown in figure 2.12 below.

RMIT- School of Computer Science and Information Technology


Introduction to Eclipse Tutorial

Page 10

Figure 2.12 Error listing generated after project is saved.

If you click the mouse twice over an error in the error list the editor will highlight the corresponding problem in
your implementation as shown below in figure 2.13.

Figure 2.13 Selecting an error from the error listing

Rectify these two errors by making the following additions to the program as shown below (in the underlined
text) and save the program again:
RMIT- School of Computer Science and Information Technology
Introduction to Eclipse Tutorial

Page 11

int number = 10;


System.out.println("Hello World");
System.println("My number is : " + number) ;
You will note that another error is reported as shown below in figure 2.14.

Figure 2.14 Second round of compilation errors

This happens because eclipse generally checks for basic syntax errors in the first compilation pass and then
evaluates statements and expressions in a second compilation pass to see if they make sense.
However it is possible that eclipse will stop checking for errors until the ones it has already detected have
been rectified, so it may take more than one pass to rectify the errors that may be present in a class/method
you have just implemented.

You can rectify this error by changing the affected line as shown below (in the underlined text):

System.out.println("My number is : " + number);


Now if you save the project then the error listing should be empty and the project is ready to be executed.

RMIT- School of Computer Science and Information Technology


Introduction to Eclipse Tutorial

Page 12

All we have left to do now is to run our sample program you can do so by selecting Run -> Run As -> Java
Application as shown below in figure 2.14.

Figure 2.14 Menu option for running a java program

Alternatively you can use the run shortcut button as shown below in figure 2.15.

Figure 2.15 Run program shortcut

RMIT- School of Computer Science and Information Technology


Introduction to Eclipse Tutorial

Page 13

You should run the program using either one of the methods described above when directed to do so
by your instructor.
Once you have used either of the above methods to run the program the java console view will appear in the
lower area of the workbench as shown below in figure 2.16.

Figure 2.16 Program output displayed on the java console

This concludes the eclipse basics tutorial - you should try go get a little more practice by writing and running
sample programs from the week 1 learning materials as well as attempting the week 1 practice exercises on
blackboard.

RMIT- School of Computer Science and Information Technology


Introduction to Eclipse Tutorial

Page 14

También podría gustarte