javac - Java programming language compiler


	javac [ options ] [ sourcefiles ] [ classes ] [ @argfiles ]

Arguments may be in any order.

Command-line options.
One or more source files to be compiled (such as MyClass.java).
One or more classes to be processed for annotations (such as MyPackage.MyClass).


The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. It can also process annotations in Java source files and classes.

There are two ways to pass source code file names to javac:

Source code file names must have .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 called MyClass would be written in a source file called MyClass.java and compiled into a bytecode class file called MyClass.class.






Standard Options

Options to pass to annotation processors. These are not interpreted by javac directly, but are made available for use by individual processors. key should be one or more identifiers separated by ".".
-cp path or -classpath path
Specify where to find user class files, and (optionally) annotation processors and source files. This classpath overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.

If the -sourcepath option is not specified, the user class path is also searched for source files.

If the -processorpath option is not specified, the classpath is also searched for annotation processors.


For example, if directory foo contains a.jar and b.JAR, then the class path element foo/* is expanded to A.jar:b.JAR, except that the order of jar files is unspecified. All jar files in the specified directory, even hidden ones, are included in the list. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. The CLASSPATH environment variable, where defined, will be similarly expanded. Note:   Depending of the configuration of your command line environment, you may have to quote the wild card character, for example, javac -cp "*.jar" MyClass.java.

Override the location of installed extensions.
Override the location of endorsed standards path.
-d directory
Set the destination directory for class files. The directory must already exist; javac will not create it. 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 /home/myclasses and the class is called com.mypackage.MyClass, then the class file is called /home/myclasses/com/mypackage/MyClass.class.

If -d is not specified, javac puts each class files in the same directory as the source file from which it was generated.


-encoding encoding
Do not generate any debugging information.
-g:{keyword list}
Source file debugging information
Line number debugging information
Local variable debugging information
Print a synopsis of standard options.
Controls the generation of class files for implicitly loaded source files. To automatically generate class files, use -implicit:class. To suppress class file generation, use -implicit:none. If this option is not specified, the default is to automatically generate class files. In this case, the compiler will issue a warning if any such class files are generated when also doing annotation processing. The warning will not be issued if this option is set explicitly. See Searching For Types.
-proc: {none,only}
Controls whether annotation processing and/or compilation is done. -proc:none means that compilation takes place without annotation processing. -proc:only means that only annotation processing is done, without any subsequent compilation.
-processor class1[,class2,class3...]
Names of the annotation processors to run. This bypasses the default discovery process.
-processorpath path
Specify where to find annotation processors; if this option is not used, the classpath will be searched for processors.
-s dir
Specify the directory where to place generated source files. The directory must already exist; javac will not create it. If a class is part of a package, the compiler puts the source file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -s /home/mysrc and the class is called com.mypackage.MyClass, then the source file will be placed in /home/mysrc/com/mypackage/MyClass.java.
-source release
Synonym for 1.5.
This is the default value. No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors, instead of warnings, as previously.
Synonym for 1.6.

-sourcepath sourcepath

Note:   Classes found through the classpath may be subject to automatic recompilation if their sources are also found. See Searching For Types.

Print version information.
Display information about non-standard options and exit.

Cross-Compilation Options


-target version


  • If -source is not specified, the value of -target is 1.6
  • If -source is 1.2, the value of -target is 1.4
  • If -source is 1.3, the value of -target is 1.4

-bootclasspath bootclasspath
-extdirs directories

Non-Standard Options

Prepend to the bootstrap class path.
Append to the bootstrap class path.
Override location of bootstrap class files.
Disable all warnings not mandated by the Java Language Specification.
Disable warning name, where name is one of the warning names supported for -Xlint:name, below.
Warn about missing serialVersionUID definitions on serializable classes.
Warn about finally clauses that cannot complete normally.
switch (x) {
case 1:
       //  No  break;  statement here.
case 2:
-Xmaxerrs number
Set the maximum number of errors to print.
-Xmaxwarns number
Set the maximum number of warnings to print.
-Xstdout filename
Specify which file to read when both a source file and class file are found for a type. (See Searching For Types). If -Xprefer:newer is used, it reads the newer of the source or class file for a type (default). If the -Xprefer:source option is used, it reads source file. Use -Xprefer:source when you want to be sure that any annotation processors can access annotations declared with a retention policy of SOURCE.
Print out textual representation of specified types for debugging purposes; perform neither annotation processing nor compilation. The format of the output may change.
Print information about which annotations a processor is asked to process.
Print information about initial and subsequent annotation processing rounds.

The -J Option








Example - Single Arg File


  % javac @argfile


Example - Two Arg Files


Create a file named "options" containing:

     -d classes
     -sourcepath /java/pubs/ws/1.3/src/share/classes

Create a file named "classes" containing:


You would then run javac with:

  % javac @options @classes

Example - Arg Files with Paths


  % javac @path1/options @path2/classes


javac provides direct support for annotation processing, superseding the need for the separate annotation processing tool, apt.

The API for annotation processors is defined in the javax.annotation.processing and javax.lang.model packages and subpackages.

Overview of annotation processing

Unless annotation processing is disabled with the -proc:none option, the compiler searches for any annotation processors that are available. The search path can be specified with the -processorpath option; if it is not given, the user class path is used. Processors are located by means of service provider-configuration files named
on the search path. Such files should contain the names of any annotation processors to be used, listed one per line. Alternatively, processors can be specified explicitly, using the -processor option.

After scanning the source files and classes on the command line to determine what annotations are present, the compiler queries the processors to determine what annotations they process. When a match is found, the processor will be invoked. A processor may "claim" the annotations it processes, in which case no further attempt is made to find any processors for those annotations. Once all annotations have been claimed, the compiler does not look for additional processors.

If any processors generate any new source files, another round of annotation processing will occur: any newly generated source files will be scanned, and the annotations processed as before. Any processors invoked on previous rounds will also be invoked on all subsequent rounds. This continues until no new source files are generated.

After a round occurs where no new source files are generated, the annotation processors will be invoked one last time, to give them a chance to complete any work they may need to do. Finally, unless the -proc:only option is used, the compiler will compile the original and all the generated source files.

Implicitly loaded source files

To compile a set of source files, the compiler may need to implicitly load additional source files. (See Searching For Types). Such files are currently not subject to annotation processing. By default, the compiler will give a warning if annotation processing has occurred and any implicitly loaded source files are compiled. See the -implicit option for ways to suppress the warning.







A successful type search may produce a class file, a source file, or both. If both are found, you can use the -Xprefer option to instruct the compiler which to use. If newer is given, the compiler will use the newer of the two files. If source is given, it will use the source file. The default is newer.

If a type search finds a source file for a required type, either by itself, or as a result of the setting for -Xprefer, the compiler will read the source file to get the information it needs. In addition, it will by default compile the source file as well. You can use the -implicit option to specify the behavior. If none is given, no class files will be generated for the source file. If class is given, class files will be generated for the source file.

The compiler may not discover the need for some type information until after annotation processing is complete. If the type information is found in a source file and no -implicit option is given, the compiler will give a warning that the file is being compiled without being subject to annotation processing. To disable the warning, either specify the file on the command line (so that it will be subject to annotation processing) or use the -implicit option to specify whether or not class files should be generated for such source files.


javac supports the new Java Compiler API defined by the classes and interfaces in the javax.tools package.



JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
int rc = javac.run(null, null, null, args);

This will write any diagnostics to the standard output stream, and return the exit code that javac would give when invoked from the command line.

You can use other methods on the javax.tools.JavaCompiler interface to handle diagnostics, control where files are read from and written to, and so on.

Old Interface

Note:   This API is retained for backwards compatibility only; all new code should use the Java Compiler API, described above.

The com.sun.tools.javac.Main class provides two static methods to invoke the compiler from a program:

public static int compile(String[] args);


The out parameter indicates where the compiler's diagnostic output is directed.

The return value is equivalent to the exit value from javac.

Note that all other classes and methods found in a package whose name starts with com.sun.tools.javac (informally known as sub-packages of com.sun.tools.javac) are strictly internal and subject to change at any time.


Compiling a Simple Program


% ls
% ls greetings
% cat greetings/Hello.java
package greetings;

public class Hello {
    public static void main(String[] args) {
            System.out.println("Hello " + args[i]);
% javac greetings/Hello.java
% ls greetings
Hello.class   Hello.java
% java greetings.Hello World Universe Everyone
Hello World
Hello Universe
Hello Everyone

Compiling Multiple Source Files

This example compiles all the source files in the package greetings.

% ls
% ls greetings
Aloha.java         GutenTag.java      Hello.java         Hi.java
% javac greetings/*.java
% ls greetings
Aloha.class         GutenTag.class      Hello.class         Hi.class
Aloha.java          GutenTag.java       Hello.java          Hi.java


Specifying a User Class Path


% pwd
% javac greetings/Hi.java


% javac -classpath /examples /examples/greetings/Hi.java


% javac -classpath /examples:/lib/Banners.jar \


% java -classpath /examples:/lib/Banners.jar greetings.Hi

Separating Source Files and Class Files


% ls
classes/  lib/      src/
% ls src
% ls src/farewells
Base.java      GoodBye.java
% ls lib
% ls classes
% javac -sourcepath src -classpath classes:lib/Banners.jar \
	    src/farewells/GoodBye.java -d classes
% ls classes
% ls classes/farewells
Base.class      GoodBye.class


Note:   The compiler compiled src/farewells/Base.java, even though we didn't specify it on the command line. To trace automatic compiles, use the -verbose option.

Cross-Compilation Example

Here we use javac to compile code that will run on a 1.5 VM.

% javac -target 1.5 -bootclasspath jdk1.5.0/lib/rt.jar \
	    -extdirs "" OldCode.java


The Java Platform JDK's javac would also by default compile against its own bootstrap classes, so we need to tell javac to compile against JDK 1.5 bootstrap classes instead. We do this with -bootclasspath and -extdirs. Failing to do this might allow compilation against a Java Platform API that would not be present on a 1.5 VM and would fail at runtime.


Oracle and/or its affiliates
Java Technology


Contact Us