Section B.1 The Java Compiler:
The Java compiler (
javac) translates Java source files into Java bytecode. A Java source file must have the
javaccompiler will create a bytecode file with the same name but with the
javaccommand 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
-classpathoption), 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,
javacwill read class definitions contained in
MyHelperClass.javain 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.javacontained an inner class named
javacwould 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
MyMainClassuses an instance of
MyHelperClass, you can compile both classes with the following command:
In this case,
javacwill perform a search for the definition of
Subsection B.1.1 How Java Searches for Class Definitions
When compiling a file,
javacneeds a definition for every class or interface that’s used in the source file. For example, if you are creating a subclass of
javacwill need definitions for all of
Panel’s superclasses, including
Component. The definitions for these classes are contained in the
javacwill search for these classes.
Javacwill first search among its library files for definitions of classes, such as
javacwill 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
-classpathoption 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
javacto search in both the current directory (.) and the
../sourcedirectory 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,
javacmay find a source file, a class file, or both. If it finds a class file but not source file
javacwill use the class file. This would be the case for Java library code. If
javacfinds 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
javacfinds 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
javacwith 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.