Introduction to Eclipse
Overview
Until now, we've used DrJava throughout your computer science education. Although DrJava with its Interactions Pane is a nice IDE
(Integrated Development Environment) for introductory purposes, it
lacks many of the features found in professional quality IDE's, such as
a good debugger and good large-scale project management capabilities. In this lab we introduce a powerful IDE called
Eclipse which
you will find to be a very useful tool, especially when working on
large projects. Like DrJava, Eclipse is free and multi-platform (unix,
Mac, Windows).
Advantages of Eclipse over DrJava
- Project management
- Good debugger
-
Error
checking features (e.g. error checking as you type, suggestions to fix compile errors, ...)
- Auto complete feature (e.g. if you type "referenceVariable.", lets you choose a method/field from the variable's class from a drop down list)
How to Set Up Eclipse
- In the Unix Labs, eclipse 3.0 is with Java 1.5 is already installed.
- For your home computers you
can download Eclipse from http://www.eclipse.org/downloads/index.php. You will need to download the version for Java Developers, not Java EE Developers.
How to Run Eclipse
- The first
time you run it, you will be asked several questions about setting up your
workspace. We suggest keeping the default settings. Once you complete
the initial setup, you will be introduced with a Welcome screen which you
can skip.
- Eclipse
is designed around views called perspectives. The first time you start Eclipse, you
will be in Resource perspective. We will be seeing Java and
Debug perspectives in this tutorial.
Working with Projects
- Your workspace is divided in to projects.
- A project is a collection
of source files, documentation, and other related files.
- Suggestion: create a separate project for each homework and lab.
- To
create a project; choose File -> New ->Project. Choose
Java Project, then choose a name and a directory.
- After creating a project, you will see it listed in the Package
Explorer section.
- If you
already have Eclipse projects, you can import them in to the current
workspac by choosing File -> Import.
Browsing a Project
- If you
expand a project in Package Explorer, you will see packages included in
the project and the libraries it uses. Default package contains every class
that is not included in any package.
- To add
libraries to your project that you will use such as provided jar files, go
to Project Properties by doing a right click on project name. From
properties screen, go to Java Build Path. Here you can add all required
libraries.
- As you expand a source file, you will see a list of
its classes, and a list of methods in
each class and so on. This can help you lookup a method quickly if needed.
You can also use the outline pane as you are writing code to have a quick
list of accessible methods.
- To
create a class or interface in a project, you can choose File -> New
-> Class or Interface. If your class is extending another class or
implementing an interface, Eclipse will automatically place method
declarations for you.
Helpful Features While Coding in the Code Pane:
- Auto
complete in methods: As you write your code, you will be presented with a
list of possible choices.
- Error
checking as you type: Errors will be underlined red in your code. One
thing to note here, eclipse will mark your code incorrect as you are
typing it. Once you complete the line, it should go away unless your code
is really incorrect.
- Easy
commenting: If you're on a method signature, you can press
Alt+Shift+J to insert javadoc comments.
- Auto indent any piece of code by selecting it and pressing Ctrl+I.
- Auto-compile: your
files will be compiled automatically each time you save them. If an error
exists in your files, you will see a red mark by the file name in
Package Explorer. Also, the Error Pane located at the bottom will give you
a list of errors occurred during last compilation. You can double click an
error to go to its location.
Running your code
- Unlike
DrJava, eclipse is only able to execute classes
with main methods. To do so, open the Context Menu on the file name and choose Run As -> Java Application. You
will see the output in Console pane.
Alternatively, you can press Alt+Shift+X, J to run the file that is
currently being edited.
- If you
want to have more options such as sending arguments, you can choose Run...
instead of Java Application.
Debugging
The eclipse debugger is powerful. To learn it we'll debug
DebugLab.java, which contains
a DebugLab class with a simple method and a very simple Useless class.
We will set breakpoints (at lines of code where we want Java to pause during execution so we can examine the values of variables and so forth).
- Create a new project and create a new class called DebugLab in the project.
- Copy the contents of the DebugLab.java< file to that class (or alternatively copy DebugLab.java< to the directory of the
project and refresh the project).
- You
will set breakpoints to suspend the execution of your program. To add a breakpoint to any line in the code, press Ctrl+Shift+B
while you are on that line. (Alternatively, choose Run -> Toggle Breakpoint from the menu.) For our purposes,
add a breakpoint to the line that has the debug method.
- For
debugging to work, you have to run your program in Debug mode. To do that
you can press Alt+Shift+D, J or choose Debug As -> Java Application from
the context menu. Once you run your program, it will execute up to first
breakpoint it encounters and stop at that point, Eclipse will ask you to
switch to Debug
perspective automatically as it happens.
- Debug
perspective contains a call stack at the top left corner, a summary of variables
in top right corner. You can also get a list of breakpoints at the same
place. At the bottom, you can see the console which will show any output.
- Our aim
will be to analyze the values of Useless object u and the integer array a.
There are several ways to do this:
- You
can highlight u in the code
panel and choose Inspect from
the context menu. This will open a small window listing all instance
variables of u.
- From
the variables panel at the top, you can expand u and get a list of its instance variables.
- As
the last option, you can add an expression to be watched. In order to do
that, you have to open Expressions
window by choosing Window ->
Show View -> Expressions. This will open a new tab on top right
corner. You can right click anywhere on that tab and choose Add Watch Expression, any
expression you type here will be watched throughout the debugging process.
For example you can type u.a to watch a field of Useless
object u. Note that this only
works as long as the object that's being watched is in scope.
- Similarly,
you can repeat all of the above to view any index of array.
- You
also have different options to continue running your code. You can access
the buttons explained below on the Debug pane.
- Resume (F8): Resumes your program
until the next breakpoint or until the end of execution.
- Step Into (F5):
Only executes one line in the program. If the current line is a method call,
it will go into that method. For example, if you do Step Into at our current breakpoint, your program will stop
at the first line of debug
method.
- Step Over
(F6): Only executes one line in the program. If the currently line is
a method call, it completes the execution of that method and goes to next
line. It doesn't go into the called method like Step Into. So if you do a step over at the current
breakpoint, it will execute debug method
and goes to next line which is the end of the program.
- At
any time, you can terminate the execution of the program by pressing Terminate button.