The Java Compiler: javac
The Java compiler (
) TRanslates Java source files into Java bytecode. A Java source file must have the
compiler will create a bytecode file with the same
The brackets in this expression
Most of the time you would simply list the sourcefiles you are compiling immediately after the word javac , as in the following example:
javac MyAppletClass.java MyHelperClass.java
Given this command, javac will read class definitions contained in MyAppletClass.java and MyHelperClass.java in the current working directory and translate them into bytecode files named MyAppletClass.class and MyHelperClass.class .
If a Java source file contains inner classes, these would be compiled into separate class files. For example, if MyAppletClass.java contained an inner class named Inner , javac would compile the code for the inner class into a file named MyAppletClass$Inner.class .
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 classthat 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 MyAppletClass 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 MyHelperClass .
How Java Searches for Class Definitions
When compiling a file, javac needs a definition for every class or interface used in the source file. For example, if you are creating a subclass of java.applet.Applet , javac will need definitions for all of Applet 's superclasses, including Panel , Container , and Component . The definitions for these classes are contained in the java.awt package. Here is how javac will search for these classes.
will first search among its library files for definitions of classes, such as
will search among the files and directories listed on the
javac -classpath . . /source:. MyApplet.java
to search in both the current directory (.) and in the
directory for user source files. Because the details for setting the CLASSPATH variable are system dependent, it is best to
During a successful search,
may find a source file, a class file, or both. If it finds a class file but not a source file,
will use the class file. This would be the case for Java library code. If
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
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
As noted earlier, if your application or applet uses several source files, you need only provide javac with the name of the main application or applet file. It will find and compile all the source files, as long as they are located in a directory listed in the class path.