Java Reference
In-Depth Information
B
compiled
by
executed
by
generates
Welcome.java
(Java source-
code file)
Welcome.class
(Java bytecode
executable file)
Java
Compiler
Any
Computer
JVM
Library Code
(a)
(b)
F IGURE 1.8
(a) Java source code is translated into bytecode. (b) Java bytecode can be executed on any computer with a
Java Virtual Machine.
Figure 1.9 shows the javac command for compiling Welcome.java . The compiler gener-
ates the Welcome.class file, and this file is executed using the java command.
javac command
java command
Note
For simplicity and consistency, all source-code and class files used in this topic are
placed under c:\book unless specified otherwise.
c:\book
Compile
Show files
VideoNote
Compile and run a Java
program
Run
F IGURE 1.9
The output of Listing 1.1 displays the message “Welcome to Java!”
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 mem-
ory 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
class loader
bytecode verifier
 
 
Search WWH ::




Custom Search