Supplement J: Eclipse Tutorial For Introduction To Java Programming, 5E by Y. Daniel Liang
Supplement J: Eclipse Tutorial For Introduction To Java Programming, 5E by Y. Daniel Liang
Supplement J: Eclipse Tutorial For Introduction To Java Programming, 5E by Y. Daniel Liang
0 Introduction
This tutorial is for students who are currently taking a
Java course that uses Eclipse and for Java programmers who
want to develop Java projects using Eclipse. Eclipse is an
open source supported by IBM.
You can use JDK command line utility to write Java programs.
The JDK command line utility consists of a set of separate
programs, such as compiler and interpreter, each of which is
invoked from a command line. Besides the JDK command line
utility, there are more than a dozen Java development tools
on the market today, including Borland JBuilder, NetBeans,
Sun ONE Studio (a commercial version of NetBeans), Eclipse,
and WebGain Visual Café. These tools support an integrated
development environment (IDE) for rapidly developing Java
programs. Editing, compiling, building, debugging, and
online help are integrated in one graphical user interface.
Using these tools effectively will greatly increase your
programming productivity.
8
© Copyright Y. Daniel Liang, 2005
NOTE: Eclipse can run on any platform with a Java
Virtual Machine. The screen shots in the tutorial
are taken from Windows using Eclipse 3.0. You can
download Eclipse from www.eclipse.org.
Figure 1
9
© Copyright Y. Daniel Liang, 2005
Figure 2
Figure 3
10
© Copyright Y. Daniel Liang, 2005
The Eclipse main window is the command center for the
IDE.
2 Choosing a Perspective
A perspective defines the initial set and layout of views in
the window. Perspectives control what appears in certain
menus and toolbars. For example, a Java perspective contains
the views that you would commonly use for editing Java
source files, while the Debug perspective contains the views
you would use for debugging Java programs. You may switch
perspectives, but you need to specify an initial perspective
for a workspace.
Figure 4
11
© Copyright Y. Daniel Liang, 2005
Figure 5
3 Creating a Project
To create a project, choose File, New, Project to display
the New Project wizard, as shown in Figure 6. Select Java
Project and click Next to display New Java Project wizard,
as shown in Figure 7. Type myjavaprograms in the Project
name field. As you type, the Directory field becomes
c:\smith\myjavaprograms. Make sure that you selected the
options Create project in workspace and Use project folder
as root for sources and class files. Click Finish to create
the project.
12
© Copyright Y. Daniel Liang, 2005
Figure 6
13
© Copyright Y. Daniel Liang, 2005
Figure 7
4 Creating a Program
Now you can create a program in the project by choosing
File, New, Class to display the New Java Class wizard, as
shown in Figure 8. Type Welcome in the Name field. Check the
option public static void main(String[] args). Click Finish
to generate the template for the source code Welcome.java,
as shown in Figure 9.
NOTE:
14
© Copyright Y. Daniel Liang, 2005
Figure 8
The New Java Class wizard lets you create a new Java
class.
15
© Copyright Y. Daniel Liang, 2005
Figure 9
16
© Copyright Y. Daniel Liang, 2005
Figure 10
17
© Copyright Y. Daniel Liang, 2005
Figure 11
Figure 12
18
© Copyright Y. Daniel Liang, 2005
Figure 13
19
© Copyright Y. Daniel Liang, 2005
Figure 14
You can run the Java program from the DOS prompt using
the java command.
7 Debugging in Eclipse
The debugger utility is integrated in Eclipse. You can
pinpoint bugs in your program with the help of the Eclipse
debugger without leaving the IDE. The Eclipse debugger
enables you to set breakpoints and execute programs line by
line. As your program executes, you can watch the values
stored in variables, observe which methods are being called,
and know what events have occurred in the program.
You can execute a program line by line to trace it, but this
is time-consuming if you are debugging a large program.
Often, you know that some parts of the program work fine. It
makes no sense to trace these parts when you only need to
trace the lines of code that are likely to have bugs. In
cases of this kind, you can use breakpoints.
20
© Copyright Y. Daniel Liang, 2005
quickly move over the sections you know work correctly and
concentrate on the sections causing problems.
Figure 15
21
© Copyright Y. Daniel Liang, 2005
2. Right-click on ShowCurrentTime.java in the project
pane to display a context menu. Choose Debug, Java
Application to start debugging. You will first see
the Confirm Perspective Switch dialog, as shown in
Figure 16. Click Yes to switch to the Debug
perspective. The UI for Debug perspective is shown in
Figure 17.
Figure 16
Figure 17
22
© Copyright Y. Daniel Liang, 2005
The program pauses at the first line in the main method.
This line, called the current execution point, is
highlighted in green. The execution point marks the next
line of source code to be executed by the debugger.
23
© Copyright Y. Daniel Liang, 2005
Figure 18
24
© Copyright Y. Daniel Liang, 2005
Figure 19
25
© Copyright Y. Daniel Liang, 2005
Figure 20
TIP:
Note:
Note:
Figure 21
26
© Copyright Y. Daniel Liang, 2005
To run an applet, choose Run, Run As, Java Applet, as shown
in Figure 22. Eclipse automatically creates an HTML file to
contain the applet and invokes the appletviewer utility to
run the applet, as shown in Figure 23.
Figure 22
Figure 23
27
© Copyright Y. Daniel Liang, 2005