javac tool reads Java source files and compiles them into Java class files. The compiler is implemented in the jdk.compiler module.
Basic Usage
Source Code
The javac implementation is located at:- Main class:
src/jdk.compiler/share/classes/com/sun/tools/javac/Main.java:1 - Options:
src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java:1 - Core compiler:
src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java:1
Standard Options
Classpath and Module Path
Specify where to find user class files and annotation processors. Can also use
-classpath or -cp.Specify where to find application modules. Can also use
-p.Specify where to find input source files. Can also use
-sourcepath.Specify where to find input source files for multiple modules.
Output Control
Specify where to place generated class files.
Specify where to place generated source files (from annotation processors).
Specify where to place generated native header files.
Source and Target Versions
Provide source compatibility with the specified Java SE release. Can also use
-source.Supported values include: 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23Generate class files suitable for the specified Java SE release. Can also use
-target.Compile for a specific Java SE release. Sets source, target, and bootstrap classpath.
Module System Options
Compile only the specified module. Can also use
-m.Root modules to resolve in addition to the initial modules.
Limit the universe of observable modules.
Export a package from a module to other modules.
Update a module to read another module.
Annotation Processing
Control annotation processing and compilation.
none: No annotation processingonly: Only annotation processing, no compilationfull: Both annotation processing and compilation (default)
Names of annotation processors to run.
Specify where to find annotation processors. Can also use
-processorpath.Debugging and Diagnostics
Generate all debugging information (default).
Generate no debugging information.
Generate only specified kinds of debugging information.
Output messages about what the compiler is doing.
Warnings and Errors
Enable all recommended warnings.
Enable or disable specific warnings. Use
-Xlint:key to enable or -Xlint:-key to disable.Available keys: cast, classfile, deprecation, dep-ann, divzero, empty, exports, fallthrough, finally, missing-explicit-ctor, module, opens, options, overloads, overrides, path, processing, rawtypes, removal, requires-automatic, requires-transitive-automatic, serial, static, try, unchecked, varargs, previewTerminate compilation if warnings occur.
Generate no warnings.
Preview Features
Enable preview language features.
Extended Options
- Maximum Errors/Warnings
- DocLint
- Other Extended
Common Usage Examples
Compile Simple Application
Compile Modular Application
Compile with Dependencies
Cross-Version Compilation
Annotation Processing
Information Options
Print a synopsis of standard options. Can also use
-help, -h, or -?.Print help on extra options. Can also use
-X.Print version information. Can also use
-version.The javac compiler supports reading options from files using where
@filename. This is useful for long command lines:options.txt contains:When compiling for older Java versions using
--release, javac automatically uses the correct API signatures to prevent using APIs not available in the target version.Exit Codes
0: Successful compilation1: Compilation errors occurred2: Invalid command-line arguments3: System error or resource exhaustion4: Abnormal termination