Skip to main content

Section B.1 The Java Compiler: javac

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 MyMainClass.java and MyHelperClass.java in the current working directory and translate them into bytecode files named MyMainClass.class and MyHelperClass.class.

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 Inner, javac would compile the code for the inner class into a file named MyMainClass$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 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:

javac MyMainClass.java

In this case, javac will perform a search for the definition of MyHelperClass.

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 java.awt.Panel, javac will need definitions for all of Panel's superclasses, including Container, and Component. The definitions for these classes are contained in the java.awt package.

Here's howjavac will search for these classes.

Javac will first search among its library files for definitions of classes, such as Panel. Next, 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 -classpath option.

For example,

javac -classpath ../source:. MyMain.java

will tell 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.

You have attempted of activities on this page.