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
you will find to be a very useful tool, especially when working on
large projects. Like DrJava, Eclipse is free and multi-platform (unix,
Advantages of Eclipse over DrJava
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.
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
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.
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
already have Eclipse projects, you can import them in to the current
workspac by choosing File -> Import.
Browsing a Project
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.
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
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.
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:
complete in methods: As you write your code, you will be presented with a
list of possible choices.
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.
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.
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
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.
want to have more options such as sending arguments, you can choose Run...
instead of Java Application.
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).
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.
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.
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.
will be to analyze the values of Useless object u and the integer array a.
There are several ways to do this:
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.
the variables panel at the top, you can expand u and get a list of its instance variables.
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.
you can repeat all of the above to view any index of array.
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
(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.
any time, you can terminate the execution of the program by pressing Terminate button.