Java Reference
In-Depth Information
Executing the
Welcome1
Application
The following instructions assume that the topic's examples are located in
C:\examples
on Windows or in your user account's
Documents/examples
folder on Linux/OS X. To
execute this program in a command window, change to the directory containing
Welcome1.java
—
C:\examples\ch02\fig02_01
on Microsoft Windows or
~/Documents/
examples/ch02/fig02_01
on Linux/OS X. Next, type
java Welcome1
and press
Enter
. This command launches the JVM, which loads the
Welcome1.class
file.
The command
omits
the
.class
file-name extension; otherwise, the JVM will
not
execute
the program. The JVM calls class
Welcome1
's
main
method. Next, the statement at line 9
of
main
displays
"Welcome
to
Java
Programming!"
. Figure 2.2 shows the program execut-
ing in a Microsoft Windows
Command Prompt
window. [
Note:
Many environments show
command windows with black backgrounds and white text. We adjusted these settings to
make our screen captures more readable.]
Error-Prevention Tip 2.4
When attempting to run a Java program, if you receive a message such as “
Exception
in
thread
"main"
java.lang.NoClassDefFoundError:
Welcome1
,” your
CLASSPATH
envi-
ronment variable has not been set properly. Please carefully review the installation in-
structions in the Before You Begin section of this topic. On some systems, you may need to
reboot your computer or open a new command window after configuring the
CLASSPATH
.
You type this
command to execute
the application
The program outputs to the screen
Welcome to Java Programming!
Fig. 2.2
|
Executing
Welcome1
from the
Command Prompt
.
In this section, we modify the example in Fig. 2.1 to print text on one line by using mul-
tiple statements and to print text on several lines by using a single statement.
Displaying a Single Line of Text with Multiple Statements
Welcome
to
Java
Programming!
can be displayed several ways. Class
Welcome2
, shown in
Fig. 2.3, uses two statements (lines 9-10) to produce the output shown in Fig. 2.1. [
Note:
From this point forward, we highlight with a yellow background the new and key features
in each code listing, as we've done for lines 9-10.]
The program is similar to Fig. 2.1, so we discuss only the changes here. Line 2
// Printing a line of text with multiple statements.