javac compiles Java programs.
javac [ options ] filename.java ... javac_g [ options ] filename.java ...
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
.javaextension. 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
trueto send output messages to
false, that is, do not set it, to send output messages to
javac_g is a non-optimized version of javac suitable for use with debuggers like jdb for debugging javac itself. Using javac_g is not equivalent to using the command javac -g.
- -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 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 .:/home/avh/classes:/usr/local/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 /home/avh/classes MyProgram.javacauses the compiled class files for the classes in the
MyProgram.javasource file to be saved in the directory
/home/avh/classes. If your classes are defined in the package demos/awt, the class files would be placed in the directory /home/avh/classes/demos/awt.
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.
- 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.
- Generate a warning for every use or override of a deprecated member or class. A member or class is deprecated if its documentation comment contains the
@deprecatedtag. The compiler will emit a warning at the end of compilation whether or not -deprecation is used; this option causes the location of each individual use or override to be noted.
Deprecated members or classes are deliberately not mentioned if the source file containing the deprecation is being recompiled. This can happen because the file is on the command line or because -depend is used and the source file was out of date.
- Turns off warnings. If used the compiler does not print out any warnings.
- Directs the compiler to try to generate faster code by inlining static, final and private methods. This option may slow down compilation, make larger class files, and/or make it difficult to debug. -O implicitly turns on -depend and turns off -g.
This option informs the compiler that all generated class files are guaranteed to be delivered and upgraded as a unit, enabling optimizations that may otherwise break binary compatibility. Use this option with discretion.
The Java Language Specification section 13.4.21 describes situations in which it is legal to use a java compiler to inline methods. The compiler will only optimize code for which source is available during the compilation, so the only .java files discoverable by the compiler should be for classes intended to be delivered or upgraded as a unit. In particular, ensure that no sources for other classes are accessible on CLASSPATH, keeping in mind that the present working directory, `.', is appended to CLASSPATH.
To ensure that a product is able to run on 1.1 as well as future binary-compatible java virtual machines, one must ensure that any sources for JDK 1.1 classes are never available along CLASSPATH while using -O.
- Causes the compiler and linker to print out messages about what source files are being compiled and what class files are being loaded.
- 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.
- 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.
Compiling One or More ClassesIn this example, the source files are located at
/home/ws/src/java/awt/*.java. Change to the directory holding the class or classes that you want to compile. Then run javac, supplying one or more class names.% cd /home/ws/src/java/awt/ % javac Button.java Canvas.javaCompiles the two classes.
- Used to provide the system a path to user-defined classes. Directories are separated by colons, for example,.:/home/avh/classes:/usr/local/java/classes
java, jdb, javah, javap, javadoc, CLASSPATH