Professional Documents
Culture Documents
Supplement I: Netbeans Tutorial For Introduction To Java Programming, 5E by Y. Daniel Liang
Supplement I: Netbeans 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 using NetBeans with Introduction to Java
Programming, 5E.
You can use the 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
This brief tutorial will help you to become familiar
with NetBeans. Specifically, you will learn how to
create projects, create programs, compile, and run
programs.
Figure 1
9
© Copyright Y. Daniel Liang, 2005
The main menu is similar to that of other Windows
applications and provides most of the commands you need to
use NetBeans, including those for creating, editing,
compiling, running, and debugging programs. The menu items
are enabled and disabled in response to the current context.
1.3 Workspaces
2 Creating a Project
A project contains information about programs and their
dependent files, and it also stores and maintains the
properties of the IDE. To create and run a program, you have
to first create a project.
When you use NetBeans for the first time, a default project
is opened. A project in NetBeans holds the information about
the project, such as program files and IDE environment
settings. You can use the default project or create your own
project. For convenience, a new project will be created to
hold all the examples in the book. Here are the steps to
create this project:
10
© Copyright Y. Daniel Liang, 2005
box, as shown in Figure 3.
3. Type Liang Intro Book in the Project Name field and
click OK to create the new project. The new project is
displayed, as shown in Figure 4. You will also see the
Filesystems window in the IDE. If the Filesystems
window is not displayed, choose Window, Filesystem.
Figure 2
Figure 3
11
© Copyright Y. Daniel Liang, 2005
Figure 4
3 File Systems
File systems are used to hold files in the project. To use
file systems, you need to first mount them. By default, no
file systems are mounted in the project, as shown in Figure
4.
12
© Copyright Y. Daniel Liang, 2005
Figure 5
Figure 6
13
© Copyright Y. Daniel Liang, 2005
Figure 7
14
© Copyright Y. Daniel Liang, 2005
Figure 8
15
© Copyright Y. Daniel Liang, 2005
Figure 9
Figure 10
16
© Copyright Y. Daniel Liang, 2005
Figure 11
Figure 12
NOTE:
17
© Copyright Y. Daniel Liang, 2005
NOTE:
Figure 13
CAUTION:
18
© Copyright Y. Daniel Liang, 2005
[BL] Choose Build, Compile, or press F9.
19
© Copyright Y. Daniel Liang, 2005
Figure 14
20
© Copyright Y. Daniel Liang, 2005
Figure 15
c:\smith Welcome.java
Welcome.class
Other files
Figure 16
21
© Copyright Y. Daniel Liang, 2005
the Output window, as shown in Figure 17.
Figure 17
22
© Copyright Y. Daniel Liang, 2005
Figure 18
Figure 19
23
© Copyright Y. Daniel Liang, 2005
Figure 20
24
© Copyright Y. Daniel Liang, 2005
Figure 21
Figure 22
25
© Copyright Y. Daniel Liang, 2005
You can use the separate windows mode interface in
NetBeans.
Figure 23
26
© Copyright Y. Daniel Liang, 2005
the content window.
The Index tab shows the index entries for the current
document. The Search tab shows the combined index entries
for all the available documents in NetBeans.
Figure 24
27
© Copyright Y. Daniel Liang, 2005
In the context menu of c:\smith in the Filesystems, choose
New, Java Package to display the Package wizard, as shown in
Figure 25.
Figure 25
Figure 26
28
© Copyright Y. Daniel Liang, 2005
Figure 27
AnotherWelcome.java
c:\smith
chapter1 AnotherWelcome.class
Welcome.java
Welcome.class
Other files
Figure 28
29
© Copyright Y. Daniel Liang, 2005
Here are the steps in running the Welcome application
created in Section 1.4 from the DOS prompt.
Figure 29
You can run the Java program from the DOS prompt using
the java command.
Figure 30
30
© Copyright Y. Daniel Liang, 2005
NOTE: On Windows 95 or Windows 98, insert the
following two lines
set path=%path%;c:\j2sdk1.5\bin
set classpath=.;%classpath%
in the autoexec.bat file to avoid setting the environment
variables in Step 2 for every DOS session. On Windows NT or
Windows 2000, select System from the Control Panel to set
the environment variables.
13 Debugging in NetBeans
The debugger utility is integrated in NetBeans. You can
pinpoint bugs in your program with the help of the NetBeans
debugger without leaving the IDE. The NetBeans 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.
31
© Copyright Y. Daniel Liang, 2005
Figure 31
32
© Copyright Y. Daniel Liang, 2005
• Attach opens a dialog box in which you can connect the
debugger to an application on another virtual machine.
This is useful for remote debugging in distributed
systems.
Figure 32
33
© Copyright Y. Daniel Liang, 2005
choosing Window, Debugging from the main menu. These panes
can be selected or deselected. The Sessions pane lists the
current debug sessions. The Breakpoints pane lists all the
breakpoints you have set. The Threads pane lists threads and
thread groups in the current debugging process. The Call
Stack pane lists the method calls that the process has made
since it began running. The Watches pane lists the variables
and expressions that are under continuous watch. The Local
Variables pane lists changes in variables and expressions as
the program is debugged. The Classes pane lists all the
classes that have been loaded by the process being debugged.
34
© Copyright Y. Daniel Liang, 2005
Figure 33
Figure 34
NOTE:
NOTE:
35
© Copyright Y. Daniel Liang, 2005
Figure 35
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.
36
© Copyright Y. Daniel Liang, 2005
the project. They are restored when you reopen it.
TIP:
37
© Copyright Y. Daniel Liang, 2005
Figure 36
38
© Copyright Y. Daniel Liang, 2005
Figure 37
Figure 38
39
© Copyright Y. Daniel Liang, 2005
Figure 39
Figure 40
40
© Copyright Y. Daniel Liang, 2005
Figure 41
Figure 42
41
© Copyright Y. Daniel Liang, 2005
Figure 43
42
© Copyright Y. Daniel Liang, 2005