Java Reference
In-Depth Information
When compiling and running a Java program, you are usually not even aware of
the fact that your program is translated into byte-code and not directly translated
into machine language code. You normally give two commands: one to compile your
program (into byte-code) and one to run your program. The run command executes
the Java Virtual Machine on the byte-code.
When you use a compiler, the terminology can get a bit confusing, because both
the input to the compiler program and the output from the compiler program are
also programs. Everything in sight is some kind of program. To make sure it is clear
which program we mean, we call the input program (which in our case will be a Java
program) the source program , or source code , and call the translated low-level-
language program that the compiler produces the object program , or object code .
The word code just means a program or a part of a program.
run
command
source code
object code
code
Class Loader
A Java program is divided into smaller parts called classes , and normally each class
definition is in a separate file and is compiled separately. In order to run your program,
the byte-code for these various classes needs to be connected together. The connecting
is done by a program known as the class loader . It is typically done automatically, so
you normally need not be concerned with it. (In other programming languages, the
program corresponding to the Java class loader is called a linker . )
Compiling a Java Program or Class
As we noted in the previous subsection, a Java program is divided into classes. Before
you can run a Java program, you must compile these classes.
Before you can compile a Java program, each class definition used in the program
(and written by you, the programmer) should be in a separate file. Moreover, the name
of the file should be the same as the name of the class, except that the file name has
.java added to the end. The program in Display 1.1 is a class called FirstProgram ,
so it should be in a file named FirstProgram.java . This program has only one class,
but a more typical Java program would consist of several classes.
If you are using an IDE (Integrated Development Environment), there will be a
simple command to compile your Java program from the editor. You will have to
check your local documentation to see exactly what this command is, but it is bound
to be very simple. (In the TextPad environment, the command is Compile Java on the
Tools menu.)
If you want or need to compile your Java program or class with a one-line command
given to the operating system, it is easy to do. We will describe the commands for
the Java system distributed by Oracle (usually called “the SDK” or “the JDK”) in the
following paragraphs.
Suppose you want to compile a class named FirstProgram . It will be in a file
named FirstProgram.java . To compile it, simply give the following command:
VideoNote
Compiling a
Java Program
.java files
javac
javac FirstProgram.java
 
Search WWH ::




Custom Search