javac - The Java Compiler

javac compiles Java programs.

SYNOPSIS

   javac [ options ] filename.java ...
   javac_g [ options ] filename.java ...

DESCRIPTION

The javac command compiles Java source code into Java bytecodes. You then use the Java interpreter - the java command - to interprete the Java bytecodes.

Java source code must be contained in files whose filenames end with the .java extension. The file name must be constructed from the class name, as classname.java, if the class is public or is referenced from another source file.

For every class defined in each source file compiled by javac, the compiler stores the resulting bytecodes in a class file with a name of the form classname.class. Unless you specify the -d option, the compiler places each class file in the same directory as the corresponding source file.

When the compiler must refer to your own classes you need to specify their location. Use the -classpath option or CLASSPATH environment variable to do this. The class path is a sequence of directories (or zip files) which javac searches for classes not already defined in any of the files specified directly as command arguments. The compiler looks in the class path for both a source file and a class file, recompiling the source (and regenerating the class file) if it is newer.

Set the property javac.pipe.output to true to send output messages to System.out. Set javac.pipe.output to false, that is, do not set it, to send output messages to System.err.

javac_g is a non-optimized version of javac suitable for use with debuggers like jdb.

OPTIONS

-classpath path
Specifies the path javac uses to look up classes needed to run javac or being referenced by other classes you are compiling. Overrides the default or the CLASSPATH environment variable if it is set. Directories are separated by semi-colons. It is often useful for the directory containing the source files to be on the class path. You should always include the system classes at the end of the path. For example:
   javac -classpath .;C:\users\dac\classes;C:\tools\java\classes ...

-d directory
Specifies the root directory of the class file hierarchy. In other words, this is essentially a destination directory for your compiled classes. For example, doing:
   javac -d C:\users\dac\classes MyProgram.java
causes the class files for the classes in the MyProgram.java source file to be saved in the directory C:\users\dac\classes.

Note that the -d and -classpath options have independent effects. The compiler reads only from the class path, and writes only to the destination directory. It is often useful for the destination directory to be on the class path. If the -d option is not specified, the source files should be stored in a directory hierarchy which reflects the package structure, so that the resulting class files can be easily located.

-encoding encoding name
Specify the source file encoding name, such as EUCJIS\SJIS. If this option is not specified, then the platform default converter is used.

-g
Enables generation of debugging tables. Debugging tables contain information about line numbers and local variables - information used by Java debugging tools. By default, only line numbers are generated, unless optimization (-O) is turned on.

-deprecation
Enables deprecation warnings. The compiler will emit a warning for each use of a deprecated member or class (unless the deprecated member or class is also being compiled or recompiled). A class or member is deprecated if it contains a documentation comment with the tag @deprecated.
-nowarn
Turns off warnings. If used the compiler does not print out any warnings.

-O
Optimizes compiled code by inlining static, final and private methods. Note that your classes may get larger in size.

-verbose
Causes the compiler and linker to print out messages about what source files are being compiled and what class files are being loaded.

-depend
Causes recompilation of class files on which the source files given as command line arguments recursively depend. Without this option, only files that are directly depended on and missing or out-of-date will be recompiled. Recompilation does not extend to missing or out-of-date files only depended on by already up-to-date class files.

-Jjavaoption
Passes through the string javaoption as a single argument to the Java interpreter which runs the compiler. The argument should not contain spaces. Multiple argument words must all begin with the prefix -J, which is stripped. This is useful for adjusting the compiler's execution environment or memory usage.

EXAMPLES

Each package name has a corresponding directory name. In the following examples, the source files are located at c:\jdk\src\java\awt\*.java.

Compiling One or More Packages

To compile a package, the source files (*.java) for that package must be located in a directory having the same name as the package. If a package name is made up of several identifiers (separated by dots), each identifier represents a different directory. Thus, all java.awt classes must reside in a directory named java\awt\.

First, change to the parent directory of the top-most package. Then run javac, supplying one or more package names.

  % cd c:\jdk\src
  % javac java.awt java.awt.event
Compiles the public classes in packages java.awt and java.awt.event.

Compiling One or More Classes

Change to the directory holding the class. Then run javac, supplying one or more class names.
  % cd c:\jdk\src\java\awt
  % javac Button.java Canvas.java
Compiles the two classes.

ENVIRONMENT VARIABLES

CLASSPATH
Used to provide the system a path to user-defined classes. Directories are separated by semi-colons, for example,
   .;C:\users\dac\classes;C:\tools\java\classes

SEE ALSO

java, jdb, javah, javap, javadoc, CLASSPATH