Team LiB
Previous Section Next Section

javacThe Java Compiler

Synopsis

javac [ options ] files

Description

javac is the Java compiler; it compiles Java source code (in .java files) into Java byte codes (in .class files). The Java compiler is itself written in Java.

javac can be passed any number of Java source files, whose names must all end with the .java extension. javac produces a separate .class class file for each class defined in the source files. Each source file can contain any number of classes, although only one can be a public top-level class. The name of the source file (minus the .java extension) must match the name of the public class it contains.

In Java 1.2 and later, if a filename specified on the command line begins with the character @, that file is taken not as a Java source file but as a list of compiler options and Java source files. Thus, if you keep a list of Java source files for a particular project in a file named project.list, you can compile all those files at once with the command:

% javac @project.list

To compile a source file, javac must be able to find definitions of all classes used in the source file. It looks for definitions in both source-file and class-file form, automatically compiling any source files that have no corresponding class files or that have been modified since they were most recently compiled.

Common options

The most commonly used compilation options include the following:


-classpath path

Specifies the path javac uses to look up classes referenced in the specified source code. This option overrides any path specified by the CLASSPATH environment variable. The path specified is an ordered list of directories, ZIP files, and JAR archives, separated by colons on Unix systems or semicolons on Windows systems. If the -sourcepath option is not set, this option also specifies the search path for source files.


-d directory

Specifies the directory in which (or beneath which) class files should be stored. By default, javac stores the .class files it generates in the same directory as the .java files those classes were defined in. If the -d option is specified, however, the specified directory is treated as the root of the class hierarchy, and .class files are placed in this directory or the appropriate subdirectory below it, depending on the package name of the class. Thus, the following command:

% javac -d /java/classes Checkers.java

places the file Checkers.class in the directory /java/classes if the Checkers.java file has no package statement. On the other hand, if the source file specifies that it is in a package:

package com.davidflanagan.games;

the .class file is stored in /java/classes/com/davidflanagan/games. When the -d option is specified, javac automatically creates any directories it needs to store its class files in the appropriate place.


-encoding encoding-name

Specifies the name of the character encoding used by the source files if it differs from the default platform encoding.


-g

Tells javac to add line number, source file, and local variable information to the output class files, for use by debuggers. By default, javac generates only the line numbers.


-g:none

Tells javac to include no debugging information in the output class files. Java 1.2 and later.


-g :keyword-list

Tells javac to output the types of debugging information specified by the comma-separated keyword-list. The valid keywords are: source, which specifies source-file information; lines, which specifies line number information; and vars, which specifies local variable debugging information. Java 1.2 and later.


-help

Prints a list of options. See also -X.


-J javaoption

Passes the argument javaoption directly through to the Java interpreter. For example: -J-Xmx32m. javaoption should not contain spaces; if multiple arguments must be passed to the interpreter, use multiple -J options. Java 1.1 and later.


-source release-number

Specifies the version of Java the code is written in. Legal values of release-number are 5, 1.5, 1.4, and 1.3. The options 5 and 1.5 are synonyms and are the default: the compiler accepts all Java 5.0 language features. Use -source 1.4 to have the compiler ignore Java 5.0 language features such as the enum keyword. Use -source 1.3 to have the compiler ignore the assert keyword that was introduced in Java 1.4. This option is available in Java 1.4 and later.


-sourcepath path

Specifies the list of directories, ZIP files, and JAR archives that javac searches when looking for source files. The files found in this source path are compiled if no corresponding class files are found or if the source files are newer than the class files. By default, source files are searched for in the same places class files are searched for. Java 1.2 and later.


-verbose

Tells the compiler to display messages about what it is doing. In particular, it causes javac to list all the source files it compiles, including files that did not appear on the command line.


-X

Tells the javac compiler to display usage information for its nonstandard options (all of which begin with -X). Java 1.2 and later.

Warning options

The following options control the generation of warning messages by javac :


-deprecation

Tells javac to issue a warning for every use of a deprecated API. By default, javac issues only a single warning for each source file that uses deprecated APIs. Java 1.1 and later. In Java 5.0, this is a synonym for -Xlint:deprecation.


-nowarn

Tells javac not to print warning messages. Errors are still reported as usual.


-Xlint

Enables all recommended warnings about program "lint." At the time of this writing, all the warnings detailed below are recommended.


-Xlint :warnings

Enables or disables a comma-separated list of named warning types. At the time of this writing, the available warning types are the following. A named warning can be suppressed by preceding it with a minus sign:


all

Enables all lint warnings.


deprecation

Warns about the use of deprecated APIs. See also -deprecation.


fallthrough

Warns when a case in a switch statement "falls through" to the next case. See also -Xswitchcheck.


finally

Warns when a finally clause cannot complete normally.


path

Warns if any path directories specified elsewhere on the command line are nonexistent.


serial

Warns about Serializable classes that do not have a serialVersionUID field.


unchecked

Provides detailed warnings about each unchecked use of a generic type.


-Xmaxerrors num

Don't print more than num errors.


-Xmaxwarns num

Don't print more than num warnings.


-Xstdout filename

Tells javac to send warning and error messages to the specified file instead of writing them to the console. Java 1.4 and later.


-Xswitchcheck

Warns about case clauses in switch statements that "fall through." In Java 5.0, use -Xlint:fallthrough.

Cross-compilation options

The following options are useful when using javac to compile class files intended to run under a different version of Java:


-bootclasspath path

Specifies the search path javac uses to look up system classes. This option does not specify the system classes used to run the compiler itself, only the system classes read by the compiler. Java 1.2 and later.


-endorseddirs path

Overrides the directories to search for endorsed standards JAR files.


-extdirs path

Specifies a list of directories to search for standard extension JAR files. Java 1.2 and later.


-target version

Specifies the class file format version to use for the generated class files. version may be 1.1, 1.2, 1.3, 1.4, 1.5, or 5. The options 1.5 and 5 are synonyms and are the default in Java 5.0, unless -source 1.4 is specified, in which case -target 1.4 is the default. Use of this flag sets the class file version number so that the resulting class file cannot be run by VMs from previous releases.


-Xbootclasspath :path

An alternative to -bootclasspath


-Xbootclasspath/a :path

Appends the specified path to the bootclasspath. Java 1.3 and later.


-Xbootclasspath/p :path

Prefixes the bootclasspath with the specified path.

Environment


CLASSPATH

Specifies an ordered list (colon-separated on Unix, semicolon-separated on Windows systems) of directories, ZIP files, and JAR archives in which javac should look for user class files and source files. This variable is overridden by the -classpath option.

See also

java, jdb

    Team LiB
    Previous Section Next Section