Section B.1 The Java Compiler:
The Java compiler (
javac) translates Java source files into Java bytecode. A Java source file must have the
.java extension. The
javac compiler will create a bytecode file with the same name but with the
.class extension. The
javac command takes the following form:
javac[ options ] sourcefiles [ files ]
The brackets in this expression indicate optional parts of the command. Thus, options is an optional list of command-line options (including the
-classpath option), and files is an optional list of files, each of which contains a list of Java source files. The files option would be used if you were compiling a very large collection of files, too large to list each file individually on the command line.
Most of the time you would simply list the sourcefiles you are compiling immediately after the word
javac, as in the following example:
javac MyMainClass.java MyHelperClass.java
Given this command,
javac will read class definitions contained in
MyHelperClass.java in the current working directory and translate them into bytecode files named
If a Java source file contains inner classes, these would be compiled into separate class files. For example, if
MyMainClass.java contained an inner class named
javac would compile the code for the inner class into a file named
If you are writing a program that involves several classes, it is not necessary to list each individual class on the command line. You must list the main class—that is, the class where execution will begin. The compiler will perform a search for all the other classes used in the main class. For example, if
MyMainClass uses an instance of
MyHelperClass, you can compile both classes with the following command:
In this case,
javac will perform a search for the definition of
Subsection B.1.1 How Java Searches for Class Definitions
When compiling a file,
javac needs a definition for every class or interface that's used in the source file. For example, if you are creating a subclass of
javac will need definitions for all of
Panel's superclasses, including
Component. The definitions for these classes are contained in the
javac will search for these classes.
Javac will first search among its library files for definitions of classes, such as
javac will search among the files and directories listed on the user's class path.
The class path is a system variable that lists all the user directories and files that should be searched when compiling a user's program. JDK no longer requires a class path variable. The class path can be set either by using the environment variable CLASSPATH or by using the
-classpath option when invoking
javac. By default, JDK will check in the current working directory for user classes. It doesn't require that the CLASSPATH variable be set. If this variable is set, it must include the current directory. The preferred way to set the classpath is by using
javac -classpath ../source:. MyMain.java
javac to search in both the current directory (.) and the
../source directory for user source files. Because the details for setting the CLASSPATH variable are system dependent, it's best to consult the online installation documentation to see exactly how this is done on your system.
During a successful search,
javac may find a source file, a class file, or both. If it finds a class file but not source file
javac will use the class file. This would be the case for Java library code. If
javac finds a source file but not a class file, it will compile the source and use the resulting class file. This would be the case for the first compilation of one of your source programs. If
javac finds both a source and a class file, it determines whether the class file is up-to-date. If so, it uses it. If not, it compiles the source and uses the resulting class file. This would be the case for all subsequent compilations of one of your source programs.
As noted earlier, if your application uses several source files, you need only provide
javac with the name of the main application file. It will find and compile all the source files, as long as they are located in a directory that's listed in the class path.