The essential steps to creating and running Java programs go as
follows:
- Create a Java source code file
- Compile the source code
- Run the compiled code in a Java Virtual Machine.
The following figure illustrates these steps:
Figure J.1.1:
Steps for creating and running a Java program.
You create the code with a text
editor and save it to a file with the ".java"
suffix. All Java source code files must end with this type name.
The first part of the name must match the class name in the source
code. In the figure the class name is Test
so you must therefore save it to the file name Test.java.
We will discuss what class actually means in later chapters.
With the javac
program, you compile this file as follows:
C:>
javac Test.java
This creates a bytecode
file (or files if the code file included more than one class)
that ends with the ".class"
type appended. Here the output is Test.class.
The bytecode consists of the instructions for the
Java Virtual Machine (JVM or just VM).
The JVM is an interpreter program that emulates a
processor that executes the bytecode instructions just as if it
were a hardware processor executing native machine code instructions.
Note: There are now
Java processors that directly execute the byte codes directly
in hardware. See Chapter
24.
The platform independence of Java thus depends on
the prior creation of JVMs for different platforms.
The Java bytecode can then run on any platform in
which the JVM is available and the program should perform the same.
This Write Once, Run Anywhere approach is a key goal of the
Java language.
Note: For applets the JVM
runs inside the browser.
References & Web Resources
Latest update: Oct. 10, 2004
|