[Chapter 16] javac

#

| Chapter 16 JDK Tools |

# javac

## Name

javac---The Java Compiler

## Availability

JDK 1.0 and later.

## 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 may 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, regardless of how many source files there are. In other words, there need not be a one-to-one mapping between Java source files and Java class files. Note also that the compiler requires that there be only a single public class defined in any one source file, and that the name of the file (minus the .java extension) be the same as the name of the class (minus its package name, of course).

By default, javac places the class files it generates in the same directory as the corresponding source file. You can override this behavior with the `-d` option.

When a source file references a class that is not defined in another source file on the command line, javac searches for the definition of that class using the class path. The default class path contains only the current directory and the system classes. You may specify additional classes and packages to be searched with the `-classpath` option or the `CLASSPATH` environment variable.

If javac compiles a source file that relies on a class that is out of date (i.e., if the source file for that class is newer than the class file), it automatically recompiles that file.

## Options

`-classpath` path

  The path that javac uses to look up classes referenced in the specified source code. This option overrides the default path and any path specified by the `CLASSPATH` environment variable. The `path` specified is an ordered list of directories and ZIP files, separated by colons on UNIX systems or semicolons on Windows systems.

  To specify additional directories or ZIP files to be searched, without overriding the default system class path, use the `CLASSPATH` environment variable. See the java reference page for more information on specifying paths.

`-d` directory

  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 file that those classes were defined in. If the `-d` flag is specified, however, the specified `directory` is treated as the root of the class hierarchy and .class files are placed in this directory, or in the appropriate subdirectory below it, depending on the package name of the class. Thus, the following command:

      % javac -d java/classes java/src/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 david.games;

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

`-depend`

  Tells javac to recompile any out-of-date class files it encounters, not just those that are referenced from one of the specified source files.

`-deprecation`

  Tells javac to issue a warning for every use of a deprecated API. By default, javac issues only a single warning if a program uses deprecated APIs. Available in JDK 1.1 and later.

`-g`

  This option tells javac to add line numbers and local variable information to the output class files, for use by debuggers. By default, javac only generates the line numbers. With the `-O` option, javac does not generate even that information.

`-J`javaoption

  Pass the argument `javaoption` directly through to the Java interpreter. `javaoption` should not contain spaces; if multiple arguments must be passed to the interpreter, use multiple `-J` options. Available in JDK 1.1 and later.

`-nowarn`

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

`-nowrite`

  Tells javac not to create any class files. Source files are parsed as usual, but no output is written. This option is useful when you want to check that a file will compile without actually compiling it.

`-O`

  Enable optimization of class files. This option may cause javac to compile `static`, `final`, and `private` methods inline, so that they execute faster. The trade-off is that the class files will be larger. This option also prevents javac from adding line number debugging information to the class files.

`-verbose`

  Tells the compiler to display messages about what it is doing.

## Environment

`CLASSPATH`

  Specifies an ordered list (colon-separated on UNIX, semicolon-separated on Windows systems) of directories and ZIP files in which javac should look for class definitions. When a path is specified with this environment variable, javac always implicitly appends the location of the system classes to the end of the path. If this environment variable is not specified, the default path is the current directory and the system classes. This variable is overridden by the `-classpath` option.

## See Also

java, jdb

java |  | javadoc