 |
As described in the Chapter 1 of the textbook,
you need an editor to enter your program (source code) initially
and make modifications to it, a compiler to turn a Java source
code program into Java bytecode, and an interpreter to execute
the bytecode. These steps are shown graphically below:
The compiler and interpreter, among other tools, are part of the Java
2 Software Development Kit. There is no editor provided with the SDK.
Your instructor may tell you to use a particular editor. Or you could
use NotePad or WordPad, which are already available on most computers
running Windows. Or you could use one of several editors that can
be found on the web.
After typing in a program using an editor, you must save the program
in a file name with a .java extension. The file name should
have the same name as the class that is stored in the file. For example,
if your class is called MyProgram, it should be stored in a
file called MyProgram.java.
Once you've prepared and stored your program, you can attempt to compile
and execute it. The Java SDK is a set of command-line tools, meaning
that you simply bring up a Command Prompt window and enter particular
commands to use the tools. The compiler is called javac (which
stands for java compiler), and the interpreter is simply called java.
In the Command Prompt window, move to the directory in which you have
stored your program. Let's assume you've stored your programs in a
directory called programs at the top level of the C: drive.
To move into this folder, you use the cd command, which stands
for change directory:
C:\> cd programs
Then you can compile the program using the javac command:
C:\programs\> javac MyProgram.java
Note that you include the .java extension on the file when
you compile it. If the compiler detects errors, they will be listed
at this point and no bytecode will be created. You'll have to go back
to the editor and fix the errors, save the program again, then return
to the Command Prompt window and recompile.
If you have a clean compile (one in which no errors are generated),
then the compiler creates a bytecode version of the class, storing
it in a file with the same file name and a .class extension.
So a clean compile of MyProgram.java will result in a new bytecode
file called MyProgram.class. You can verify the existance of
the bytecode file by executing a dir (directory listing) command.
Note: do not attempt to edit or view a bytecode file, which is in
a binary format and is not human readable.
To execute the program, submit the bytecode to the interpreter. Do
not include the .class extension when using the interpreter.
For example:
C:\programs\> java MyProgram
At this point, you may have run-time errors or logical errors (see
Chapter 1 of the book), in which case you'll have to go back to the
editor and fix the program. Always test your programs thoroughly to
try to find any problems that exist.
If you have other questions about using the Java SDK, ask your instructor.
|
|
 |