Survey
Let's begin the lab by collecting a little information from you. (If you missed your lab session, please take a few minutes to fill out the survey on your own. Your answers will help us tune the course so that it works better for you!)
Introduction to Eclipse
Overview
Up until now, we've used DrJava throughout CIS
120. 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. 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).
Is Eclipse Required for this Course?
No, it's optional: you are free to use whatever IDE you prefer for the rest of the course. But we strongly recommend that you take the trouble now to come to grips with it. You may find it a bit confusing initially, but it is really not hard to use in a basic way, and it is much more pleasant and helpful than DrJava once you're used to it.
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 the one for Java EE Developers.
A First Taste
- Start Eclipse
- Since this is the first time you're running Eclipse, you'll be asked where you want your "workspace" to live. Accepting the default is fine.
- Since this is a new workspace, Eclipse will begin by displaying a "Welcome" screen.
- To get a first impression of how Eclipse works, click on the "Tutorials" button on the welcome screen, choose the first tutorial (Hello World), and follow the instructionsto create a complete (though tiny) Java program and run it.
- Now let's do the same thing again with a more interesting program.
- Close the Welcome screen if you have it open.
- Choose File -> New -> Java Project from the menu, to create a new project in your workspace. (A project is a collection
of source files, documentation, and other related files. You should create a separate project for each homework and lab.)
- Call the new project HW2.
- Instead of "Create new project in workspace," select "Create project from existing source."
- Click the Browse button and navigate to the directory where your solution to Homework 2 is stored.
- If your completed HW2 assignment is not on the machine you're working on right now, you can just re-download the files hw2.jar and Tester.java from the course homework page.
- Click Finish to create and populate the project.
- In the "Package Explorer" (the pane on the left of the screen) you'll see your new project displayed with a small red X. This means that there is an error somewhere. To see where the error is, open the project (by double-clicking on it, or by clicking the small triangle symbol to its left). Keep opening things until you can see the individual classes in the project. If you look at one of these (by double-clicking it), you'll see that the errors are coming from places where the code is referring to resources that we provided for you in the hw2.jar file. We haven't told Eclipse to look in this file yet.
- Go to the Project Properties screen (menu Project -> Properties). Choose Java Build Path; then click the "Add external Jars" button. Navigate to the hw2.jar file on your disk and click OK (twice).
- Look back at the classes in your HW2 project and observe that the small Xs are gone. If your HW2 code was in a working state when we started, there should be no Xs left.
- To run your program, do this:
- From the menu, choose Run -> Run.
- Click Java Application.
- A "Console" window should now open and show you the results of running the Tester—just the same as what you would have seen if you had run it from a command line.
- Try adding a couple of new classes, by doing File -> New -> Class. (For example, try adding a Merchant class that extends the class Character and overrides one of its methods in some simple way.)
- In the dialog that opens, you must provide the name of the new class, but that's the only bit that's required.
Learning More
- The "Welcome Screen" that we skipped at the beginning is an excellent starting point for exploring further. You can get it back by choosing "Welcome" from the Help menu. Spend a few minutes now skimming the Overview section.
More Information
Perspectives
- 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 also be seeing Java and
Debug perspectives in this tutorial.
Browsing a Project
- If you
expand a project in the Package Explorer (the pane on the left of the screen), you will see the packages included in
the project and the libraries it uses. The "default" package contains every class
that is not included in any other 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 File -> New
->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
Driver.java, a simple program (with
unfortunate bugs) for printing some information about movie tickets.
You'll also need Ticket.java and MovieTicket.java.
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 Driver in the project.
- Copy the contents of the Driver.java file to that class (or
alternatively copy Driver.java to the directory of the project
and refresh the project). Do the same for Ticket and
MovieTicket.
- 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 first line of the main method in Driver.
- 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 various objects to see what is
happening to them as the program executes.
There are several ways to do this:
- You
can highlight the name of an object in the code
panel and choose Inspect from
the context menu. This will open a small window listing all its instance
variables.
- From
the variables panel at the top, you can expand the name of an object to 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 audience[3] to watch the fourth
element of the audience array. Note that this only
works as long as the object being watched is in scope.
- 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 enter that method.
- Step Over
(F6): Only executes one line in the program. If the current line is
a method call, it completes the execution of that method and
goes to the next
line. It doesn't go into the called method like Step Into.
- At
any time, you can terminate the execution of the program by pressing Terminate button.