Java Reference
In-Depth Information
You should be in the same directory (folder) as the file
FirstProgram.java
when you
give this
javac
command. To compile any Java class, whether it is a full program or
not, the command is
javac
followed by the name of the file containing the class.
When you compile a Java class, the resulting byte-code for that class is placed in a
file of the same name, except that the ending is changed from
.java
to
.class
. So,
when you compile a class named
FirstProgram
in the file
FirstProgram.java
, the
resulting byte-code is stored in a file named
FirstProgram.class
.
.class
files
Running a Java Program
A Java program can consist of a number of different classes, each in a different file.
When you run a Java application program, only run the class that you think of as the
program; that is, the class that contains a
main
method. Look for the following line,
which starts the
main
method:
public static void
main(String[] args)
The critical words to look for are
public static void main
. The remaining portion
of the line might be spelled slightly different in some cases.
If you are using an IDE, you will have a menu command that can be used to run a
Java program. You will have to check your local documentation to see exactly what this
command is. (In the TextPad environment, the command is Run Java Application on
the Tools menu.)
If you want or need to run your Java program with a one-line command given
to the operating system, then (in most cases) you can run a Java program by giving
the command
java
followed by the name of the class containing the
main
method.
For example, for the program in Display 1.1, you would give the following one-line
command:
java FirstProgram
Note that when you run a program, you use the class name, such as
FirstProgram
,
without any
.java
or
.class
ending.
When you run a Java program, you are actually running the Java byte-code
interpreter on the compiled version of your program. When you run your program,
the system will automatically load in any classes you need and run the byte-code
interpreter on those classes as well.
We have been assuming that the Java compiler and related software are already set
up for you. We are also assuming that all the files are in one directory. (Directories are
also called
folders
.) If you need to set up the Java compiler and system software, consult
the manuals that came with the software. If you wish to spread your class definitions
across multiple directories, that is not difficult, but we will not concern ourselves with
that detail until later .