javac [ options ] [ source.java | @filelist ] ...
The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files.There are two ways to pass source code file names to javac:
Source code file names must have
- For small number of source files, simply list the file names on the command line.
- For a large number of source files, list the the file names in a file, separated by blanks or line breaks. Then use the list file name on the javac command line, preceded by an @ character.
.java
suffixes, class file names must have.class
suffixes, and both source and class files must have root names that identify the class. For example, a class calledMyClass
would be written in a source file calledMyClass.java
and compiled into a bytecode class file calledMyClass.class
.Inner class definitions produce additional class files. These class files have names combining the inner and outer class names, such as
MyClass$MyInnerClass.class
.You should arrange source files in a directory tree that reflects their package tree. For example, if you keep all your source files in d:\workspace, the source code for
com.mysoft.mypack.MyClass
should be in d:\workspace\com\mysoft\mypack\MyClass.java.By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with -d (see Options, below).
When compiling a source file, the compiler often needs information about a type it does not yet recognize. The compiler needs type information for every class or interface used, extended, or implemented in the source file. This includes classes and interfaces not explicitly mentioned in the source file but which provide information through inheritance.For example, when you subclass java.applet.Applet, you are also using Applet's ancestor classes: java.awt.Panel, java.awt.Container, java.awt.Component, and java.awt.Object.
When the compiler needs type information, it looks for a source file or class file which defines the type. The compiler searches first in the bootstrap and extension classes, then in the user class path. The user class path is defined by setting the CLASSPATH environment variable or by using the -classpath command line option. (For details, see Configuration of JDK 1.2 Software under Win32). If you use the -sourcepath option, the compiler searches the indicated path for source files; otherwise the compiler searches the user class path both for class files and source files.
You can specify different bootstrap or extension classes with the -bootclasspath and -extdirs options. See Cross Compilation, below.
A successful type search may produce a class file, a source file, or both. Here is how javac handles each situation:
Note that javac can silently compile source files not mentioned on the command line. Use the -verbose option to trace automatic compilation.
- Search produces a class file but no source file: javac uses the class file.
- Search produces a source file but no class file: javac compiles the source file and uses the resulting class file.
- Search produces both a source file and a class file: javac determines whether the class file is out of date. If the class file is out of date, javac recompiles the source file and uses the updated class file. Otherwise, javac just uses the class file.
By default, javac considers a class file out of date only if it is older than the source file. The -depend option specifies a slower but more reliable procedure.
Javac is itself a Java application, and runs on a Java platform implementation. By default, classes are compiled against the bootstrap and extension classes in that same implementation. But javac also supports cross-compiling, where classes are compiled against a bootstrap and extension classes of a different Java platform implementation. To specify target implementation bootstrap and extension classes, use the -bootclasspath and -extdirs.Note that CLASSPATH, -classpath, -bootclasspath, and -extdirs do not specify the classes used to run javac. Fiddling with the implementation of the compiler in this way is usually pointless and always risky. If you do need to do this, use the -J option to pass through options to the underlying java launcher.
- -bootclasspath bootclasspath
- Cross-compile against the specified set of boot classes. As with the user class path, boot class path entries are separated by semicolons (;) and can be directories, JAR archives, or ZIP archives.
- -classpath classpath
- Set the user class path, overriding the user class path in the CLASSPATH environment variable. If neither CLASSPATH or -classpath is specified, the user class path consists of the current directory. See Configuration of JDK 1.2 Software under Win32 for more details.
If the -sourcepath option is not specified, the user class path is searched for source files as well as class files.
- -d directory
- Set the destination directory for class files. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d c:\myclasses and the class is called
com.mypackage.MyClass
, then the class file is calledc:\myclasses\com\mypackage\MyClass.class
.If -d is not specified, javac puts the class file in the same directory as the source file.
Note that the directory specified by -d is not automatically added to your user class path.
- -depend
- Use dependency information in a class file's constant table to determine if the class file is out of date. This is a more reliable procedure for finding classes that need to be recompiled, but can slow down the compilation process drastically.
- -deprecation
- Show a description of each use or override of a deprecated member or class. Without -deprecation, javac shows the names of source files that use or override deprecated members or classes.
- -encoding
- Set the source file encoding name, such as
EUCJIS/SJIS
. If -encoding is not specified, the platform default converter is used.
- -extdirs directories
- Cross-compile against the specified extension directories. Directories is a semicolon-separated list of directories. Each JAR archive in the specified directories is searched for class files.
- -g
- Generate local variable information for debugging.
In previous releases, the -g and -O options could not be used togther. In this release, you can combine -g and -O, but you may get suprising results, such as missing variables or relocated or missing code.
- -g:nodebug
- Do not generate any debugging information. If -g:nodebug is not specified, line number information is generated, even if -g is not specificied.
- -Joption
- Pass option to the java launcher called by javac. For example, -J-Xms12m sets the startup memory to 12 megabytes.
- -J-Djavac.pipe.output=true
- Send compiler messages to
System.out
. By default, compiler messages go toSystem.err
.
- -nowarn
- Disable warning messages.
- -O
- Optimize code for execution time. Using the -O option may slow down compilation, produce larger class files, and make the program difficult to debug.
In this release, -O does not automatically turn on -depend or turn off -g. Also, -O no longer enables inlining across classes. See -O:interclass.
- -O:interclass
- Optimize code and assume all generated classes will be delivered and upgraded as a single unit. This enables inlining across classes.
Use -O:interclass with discretion. Interclass optimization can break binary compatility. The compiler must not have access to source code for classes outside the delivery unit. In particular, the compiler must not have access to source code for the Core API, since that would produce a program incompatible with other Java platform implementations. Check for unwanted source code in the current directory, the directories specified by -sourcepath and the user class path.
- -sourcepath sourcepath
- Specify the source code path to search for class or interface definitions. As with the user class path, source path entries are separated by semicolons (;) and can be directories, JAR archives, or ZIP archives. If packages are used, the local path name within the directory or archive must reflect the package name.
Note that classes found through the classpath are subject to automatic recompilation.
- -verbose
- Verbose output. This includes information about each class loaded and each source file compiled.
One source file,Hello.java
, defines a class called greetings.Hello. Thegreetings
directory is the package directory both for the source file and the class file and is off the current directory. This allows us to use the default user class path. It also makes it unnecessary to specify a separate destination directory with -d.D:\examples>dir /b greetings D:\examples>dir /b greetings Hello.java D:\examples>type greetings\Hello.java package greetings; public class Hello { public static void main(String[] args) { for (int i=0; i < args.length; i++) { System.out.println("Hello " + args[i]); } } } D:\examples>javac greetings\Hello.java D:\examples>dir /b greetings Hello.java Hello.class D:\examples>java greetings.Hello World Universe Everyone Hello World Hello Universe Hello Everyone
This example compiles all the source files in the packagegreetings
.D:\examples>ls greetings/ D:\examples>dir /b greetings Aloha.java GutenTag.java Hello.java Hi.java D:\examples>javac greetings\*.java D:\examples>dir /b greetings Aloha.class GutenTag.class Hello.class Hi.class Aloha.java GutenTag.java Hello.java Hi.java
SinceD:\examples>javac greetings\Hi.java
greetings.Hi
refers to other classes in the
greetings
package, the compiler needs to find these other
classes. The example above works, because our default user class path
happens to be the directory containing the package directory. But
suppose we want to recompile this file and not worry about which
directory we're in? Then we need to add D:\examples
to the
user class path. We can do this by setting CLASSPATH, but here
we'll use the -classpath option.
If we changeC:\>javac -classpath d:\examples d:\examples\greetings\Hi.java
greetings.Hi
again, to use a banner utility,
that utility also needs to be accessible through the user class path.
To execute a class inC:\>javac -classpath d:\examples;d:\lib\Banners.jar d:\examples\greetings\Hi.java
greetings
, we need access both to
greetings
and to the classes it uses.
C:\>java -classpath d:\examples;d:\lib\Banners.jar greetings.Hi
It often makes sense to keep source files and class files in separate directories, especially on large projects. We use -d to indicate the separate class file destination. Since the source files are not in the user class path, we use -sourcepath to help the compiler find them.Note that the compiler compiledD:\project>dir /b classes lib src D:\project>dir /b src farewells D:\project>dir /b src\farewells Base.java GoodBye.java D:\project>dir /b lib Banners.jar D:\project>dir /b classes D:\project>javac -sourcepath src -classpath classes:lib\Banners.jar src\farewells\GoodBye.java -d classes D:\project>dir /b classes farewells D:\project>dir /b classes\farewells Base.class GoodBye.classsrc\farewells\Base.java
, even though we didn't specify it on the command line. To trace automatic compiles, use the -verbose option.
Here we use the JDK 1.2 javac to compile against the JDK 1.1 bootstrap classes. JDK 1.2 javac requires the 1.2 bootstrap classes to run, so we need to tell javac not to compile against its own Java platform. We do this with -bootclasspath and -extdirs.D:\oldies>javac -bootclasspath lib11\classes.zip -extdirs "" OldCode.java