Bonus Chapter 1: Using Eclipse
Bonus Chapter 1: Using Eclipse
Bonus Chapter 1: Using Eclipse
In This Chapter
✓ Understanding Eclipse projects and Workbenches
✓ Creating a Java project
✓ Compiling, running, and debugging with Eclipse
✓ Refactoring with Eclipse
If you’re using Windows, you may want to add a desktop shortcut for Eclipse
to make it more convenient to start. To do that, open the folder that contains
the eclipse.exe file, right-click the file and drag it to the desktop, release the
mouse button, and choose Create Shortcut from the menu that appears.
Then you can start Eclipse by double-clicking this desktop shortcut.
Many of the techniques that I describe in this chapter won’t make much
sense to you until you know how to use the Java programming features to
which they apply. (The information about how to create a new Java class
file, for example, won’t make much sense until you read about creating Java
classes in Book III.) As you find out about Java programming features in
later chapters, you may want to refer to this chapter to read about related
Eclipse features.
Figure 1-1:
The Eclipse
Workbench
window.
Figure 1-2:
Debugging
a program in
Eclipse.
Understanding Projects
An Eclipse project is a set of Java files that together build a single Java
program. Although some simple Java programs consist of just one file, most
real-world Java programs are made up of more than one Java program file.
In fact, a complicated Java program may require hundreds of Java program
files. When you work on programs that require more than one file, Eclipse
lets you treat those files as a single project.
A project consists not just of Java source files, but also the class files that are
created when you compile the project and any other files that the program
requires. Those files might include data or configuration files, as well as
read-me files, program documentation, image or sound files, and so on.
All the files for a project are stored together in a project folder, which may
include subfolders if necessary. In addition to the files required by the
program, the project folder includes files that are created by Eclipse to
store information about the project itself. A file named .project stores
descriptive information about the project, for example, and a file named
.classpath stores the locations of the classes used by the project.
All your project folders are stored in a folder called the workspace. Each
time you start Eclipse, a dialog box appears, asking for the location of the
workspace folder. If you want to change to a different workspace, choose
File➪Switch Workspace.
✦ For simple projects that have just a few Java source files, you can create
a project that stores all the project’s Java files in a single folder. Then,
when those files are compiled, the resulting class files are stored in this
same folder. This type of project is the easiest to work with, and it’s ideal
for small and medium-size projects.
✦ For large projects — those that involve dozens or even hundreds of Java
source files — you can create a project that uses one or more subfolders
to store source files. Then you’re free to create whatever subfolders you
want to help you organize your files. You might create one subfolder for
user interface classes, another for database access classes, and a third
for image files displayed by the application.
Eclipse’s File menu offers an Open File command, but it isn't very useful.
Instead, Package Explorer View (on the left side of the Java perspective; refer
to Figure 1-1) displays a list of all the Java projects in your workspace. When
you start Eclipse, the project you were working on last appears onscreen
automatically. You can switch to a different project by right-clicking the
project in Package Explorer View and then choosing Open Project, and you
can open an individual file in a project by double-clicking the file in Package
Explorer View.
1. Start Eclipse, and click OK when the Workspace Launcher dialog box
appears.
The Workspace Launcher dialog box asks for the location of your
workspace folder; in most cases, the default location is acceptable.
When you click OK, Eclipse opens with the Java perspective, with no
projects or files displayed, as shown in Figure 1-3.
Figure 1-3:
Eclipse
waits for
you to
create a
project.
Figure 1-4:
The New
Java Project
dialog box.
Figure 1-5:
The New
Java Class
dialog box.
Figure 1-6:
The newly
created
HelloApp
class.
Note: If a Save Resources dialog box appears before the program runs,
click OK. Then the program runs.
Figure 1-7:
The
HelloApp
program in
Eclipse.
Unless you’ve already read Book III, you may get bewildered early on by the
code that’s presented in this procedure. Don’t worry; this code will make
complete sense after you read about creating your own classes.
3. Click Finish.
A new class file named HelloSayer is created, and Eclipse opens it in a
Java editor.
4. Add declarations for two public fields named greeting and addressee.
Add these lines immediately after the line that declares the HelloSayer
class. Then, other than the comments and the package statement that
appears at the beginning of the class, the class looks like this:
public class HelloSayer {
Figure 1-8:
Eclipse can
create cons-
tructors
for you
auto-
matically.
7. Click the HelloApp.java tab at the top of the Java editor pane.
(Refer to Figure 1-8.) The HelloApp.java file comes to the front so that
you can edit it.
8. Edit the main method so that it uses the new HelloSayer class.
To do so, delete the code that was in the main method and replace it
with this code:
public static void main(String[] args) {
HelloSayer h =
new HelloSayer("Hello", "World!");
h.sayHello();
}
The entire HelloApp.java class now looks like Listing 4-2. Eclipse
generated all the code except the two lines in the main method.
/**
* @param args
*/
public static void main(String[] args) {
HelloSayer h =
new HelloSayer("Hello", "World");
h.sayHello();
}
Running a Program
After you enter the source code for your Eclipse project, you can run it to
see whether it works as expected. Eclipse gives you several ways to run a
Java program:
✦ In Package Explorer View, select the source file for the class you want to
run and then choose Run➪Run As➪Java Application.
✦ Right-click the source file for the class you want to run and then choose
Run➪Java Application from the shortcut menu that appears.
✦ Select the source file in Package Explorer View, click the Run button (shown
in the margin), and choose Run As➪Java Application from the menu that
appears. (If you recently ran the program, you can also choose the program
from the list of recently run programs that appears in this menu.)
When the program runs, its console output is displayed in a Console View
that appears below the Java Editor pane, as shown in Figure 1-9.
Note: If the program uses Swing to create a window, that window is displayed
separately, not within the Eclipse Workbench window. (See Book VI for more
on Swing.)
Figure 1-9:
Console
View dis-
plays the
program’s
console
output.
Fortunately, Eclipse has a powerful debugger that can help you find the
cause of either type of bug and fix it. To start the debugger, run your
program by choosing Project➪Debug As➪Java Application instead of
Project➪Run As➪Java Application. Alternatively, click the Debug button
in the Workbench toolbar, shown in the margin. Eclipse switches to Debug
perspective, as shown in Figure 1-10, and runs the program in debug mode.
Figure 1-10:
Debug per-
spective lets
you debug
errant Java
programs.
Note that when you activate the debugger, the Windows firewall may display
a security alert. If so, click Allow Access to let the debugger start. Also
note that you may want to set a breakpoint in your code before starting the
debugger. Otherwise your program may run to completion and never give
you the chance to work in the debugger. You read how to set a breakpoint in
the next section, “Stepping through your programs.”
The following sections describe some of the key features of the Debug
perspective that are useful for tracking down and correcting bugs in your
Java programs.
In Eclipse, the Debug View section of Debug perspective is where you control
the execution of the program you’re debugging. This view displays a tree
that indicates each of the threads in your program. (If you don’t know what
threads are, don’t worry about it. Most console-based programs, such as the
BugApp program shown in Figure 1-10 earlier in this chapter, use only one
thread apiece anyway. In Book V, you find out how to code programs that
use more than one thread.)
Before you can control the execution of a thread, you must suspend the
thread so that its statements stop executing. In general, you can suspend a
thread for debugging in these ways:
When you suspend a thread, the statement that will be executed next is
highlighted in the Java editor. Then you can continue the thread’s execution
one or more statements at a time by clicking the buttons at the top of Debug
View. Table 1-1 describes the most commonly used buttons.
Examining variables
When a thread is suspended, you can examine its variables to see whether
they’re set to the values you expect. In many cases, you can discover pro-
gramming errors. If you think that a variable named customerFirstName
should contain a customer’s first name, for example, and it instead contains
the name of the state in which the customer lives, you can conclude that
you didn’t assign the variable’s value properly. (This situation might be
ambiguous if the customer happens to be named Indiana Jones, of course.)
The easiest way to examine the value of a variable is simply to point the
mouse at the variable in the Java editor. Figure 1-11 shows how the value of
the variable i appears when you hover the mouse pointer over it. Here the
pop-up message indicates that the variable i has a value of 0. (This message
might be a clue as to why the program has thrown a divide-by-zero exception.)
Figure 1-11:
Displaying
a variable
value.
You can also inspect variables by using Variables View, as shown in Figure 1-12.
Each variable is listed on a separate line in the top part of Variables View. In
addition, the bottom part (called the Detail pane) displays the value of the cur-
rently selected variable. Note that as you step through the various statements
in your program, variables appear in Variables View as they are declared and
disappear from view when they go out of scope.
Figure 1-12:
Variables
View shows
the value
of each
variable.
Setting breakpoints
A breakpoint is a line in your program where you want the program to be
suspended. Setting a breakpoint allows you to efficiently execute the portions
of your program that are working properly and stop the program when it
reaches the lines that you believe to be in error.
All the breakpoints in your program are listed in Breakpoints View, shown in
Figure 1-13.
Figure 1-13:
Breakpoints
View is
where you
control
breakpoints.
The following paragraphs describe some of the ways you can work with
breakpoints in this view:
A few Refactor menu commands are especially useful as you work your way
through the basics of Java: