Java Reference
In-Depth Information
Caution
Do not use the extension
.class
in the command line when executing the program.
Use
java ClassName
to run the program. If you use
java ClassName.class
in
the command line, the system will attempt to fetch
ClassName.class.class
.
java ClassName
Tip
If you execute a class file that does not exist, a
NoClassDefFoundError
will occur.
If you execute a class file that does not have a
main
method or you mistype the
main
method (e.g., by typing
Main
instead of
main
), a
NoSuchMethodError
will occur.
NoClassDefFoundError
NoSuchMethodError
Note
When executing a Java program, the JVM first loads the bytecode of the class to memory
using a program called the
class loader.
If your program uses other classes, the class loader
dynamically loads them just before they are needed. After a class is loaded, the JVM uses
a program called the
bytecode verifier
to check the validity of the bytecode and to ensure
that the bytecode does not violate Java's security restrictions. Java enforces strict security
to make sure that Java class files are not tampered and do not harm your computer.
class loader
bytecode verifier
Pedagogical Note
Your instructor may require you to use packages for organizing programs. For example,
you may place all programs in this chapter in a package named
chapter1.
For instructions
on how to use packages, see Supplement I.F, Using Packages to Organize the Classes in
the Text.
use package
1.33
What is the Java source filename extension, and what is the Java bytecode file-
name extension?
✓
✓
Check
Point
1.34
What are the input and output of a Java compiler?
1.35
What is the command to compile a Java program?
1.36
What is the command to run a Java program?
1.37
What is the JVM?
1.38
Can Java run on any machine? What is needed to run Java on a computer?
1.39
If a
NoClassDefFoundError
occurs when you run a program, what is the cause
of the error?
1.40
If a
NoSuchMethodError
occurs when you run a program, what is the cause of the
error?
You can display text in a graphical dialog box.
Key
Point
The program in Listing 1.1 displays the text on the console, as shown in Figure 1.17. You can
rewrite the program to display the text in a message dialog box. To do so, you need to use the
showMessageDialog
method in the
JOptionPane
class.
JOptionPane
is one of the many
predefined classes in the Java library that you can reuse rather than “reinvent the wheel.” You
can use the
showMessageDialog
method to display any text in a message dialog box, as
shown in Figure 1.18. The new program is given in Listing 1.4.
JOptionPane
showMessageDialog
L
ISTING
1.4
WelcomeInMessageDialogBox.java
1
/* This application program displays Welcome to Java!
2
block comment
* in a message dialog box.
3
*/