Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/openjdk/jdk/llms.txt

Use this file to discover all available pages before exploring further.

The javac tool reads Java source files and compiles them into Java class files. The compiler is implemented in the jdk.compiler module.

Basic Usage

javac [options] [source files]

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

--class-path
path
Specify where to find user class files and annotation processors. Can also use -classpath or -cp.
javac -cp lib/commons.jar:lib/utils.jar MyClass.java
--module-path
path
Specify where to find application modules. Can also use -p.
javac --module-path mods -d build/modules src/module-info.java src/com/example/*.java
--source-path
path
Specify where to find input source files. Can also use -sourcepath.
javac -sourcepath src -d build src/com/example/Main.java
--module-source-path
module-pattern
Specify where to find input source files for multiple modules.
javac --module-source-path "src/*" -d build $(find src -name '*.java')

Output Control

-d
directory
Specify where to place generated class files.
javac -d build/classes src/**/*.java
-s
directory
Specify where to place generated source files (from annotation processors).
javac -s generated -d build MyAnnotatedClass.java
-h
directory
Specify where to place generated native header files.
javac -h native/include -d build NativeClass.java

Source and Target Versions

--source
release
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, 23
javac --source 17 --target 17 OldCode.java
--target
release
Generate class files suitable for the specified Java SE release. Can also use -target.
javac --target 11 -d build src/*.java
--release
release
Compile for a specific Java SE release. Sets source, target, and bootstrap classpath.
javac --release 17 src/com/example/*.java

Module System Options

--module
module-name
Compile only the specified module. Can also use -m.
javac --module com.example.myapp --module-path mods
--add-modules
module-list
Root modules to resolve in addition to the initial modules.
javac --add-modules java.sql,java.xml -d build src/*.java
--limit-modules
module-list
Limit the universe of observable modules.
javac --limit-modules java.base,java.logging -d build src/*.java
--add-exports
module/package=target-module
Export a package from a module to other modules.
javac --add-exports java.base/sun.nio.ch=ALL-UNNAMED MyClass.java
--add-reads
module=target-module
Update a module to read another module.
javac --add-reads com.example.app=com.example.utils src/*.java

Annotation Processing

-proc
none|only|full
Control annotation processing and compilation.
  • none: No annotation processing
  • only: Only annotation processing, no compilation
  • full: Both annotation processing and compilation (default)
javac -proc:none src/*.java
-processor
class-list
Names of annotation processors to run.
javac -processor com.example.MyProcessor,com.example.OtherProcessor src/*.java
--processor-path
path
Specify where to find annotation processors. Can also use -processorpath.
javac -processorpath processors.jar -processor MyProcessor src/*.java

Debugging and Diagnostics

-g
none
Generate all debugging information (default).
javac -g src/*.java
-g:none
none
Generate no debugging information.
javac -g:none src/*.java
-g:
lines,vars,source
Generate only specified kinds of debugging information.
javac -g:lines,source src/*.java
-verbose
none
Output messages about what the compiler is doing.
javac -verbose src/com/example/Main.java

Warnings and Errors

-Xlint
none
Enable all recommended warnings.
javac -Xlint src/*.java
-Xlint:
key-list
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, preview
javac -Xlint:unchecked,deprecation src/*.java
javac -Xlint:-serial src/*.java
-Werror
none
Terminate compilation if warnings occur.
javac -Werror -Xlint:all src/*.java
-nowarn
none
Generate no warnings.
javac -nowarn src/*.java

Preview Features

--enable-preview
none
Enable preview language features.
javac --enable-preview --source 23 PreviewFeature.java

Extended Options

-Xmaxerrs
number
Set the maximum number of errors to print.
javac -Xmaxerrs 5 src/*.java
-Xmaxwarns
number
Set the maximum number of warnings to print.
javac -Xmaxwarns 10 src/*.java

Common Usage Examples

Compile Simple Application

# Compile single file
javac HelloWorld.java

# Compile multiple files
javac *.java

# Compile with output directory
javac -d build src/**/*.java

Compile Modular Application

# Compile module with module-info.java
javac -d mods/com.example.app \
      --module-source-path src \
      $(find src/com.example.app -name '*.java')

# Compile multiple modules
javac -d mods \
      --module-source-path src \
      --module com.example.app,com.example.utils

Compile with Dependencies

# With classpath
javac -cp lib/gson.jar:lib/commons-lang3.jar \
      -d build \
      src/com/example/*.java

# With module path
javac --module-path mods:libs \
      --add-modules com.google.gson \
      -d build \
      src/module-info.java src/com/example/*.java

Cross-Version Compilation

# Compile for Java 11 compatibility
javac --release 11 -d build src/*.java

# Compile with specific source and target
javac --source 17 --target 17 -d build src/*.java

Annotation Processing

# Run annotation processors
javac -processor lombok.launch.AnnotationProcessorHider$AnnotationProcessor \
      -cp lombok.jar \
      -d build \
      src/*.java

# Generate source to specific directory
javac -s generated \
      -processor com.example.CodeGenerator \
      -d build \
      src/*.java

Information Options

--help
none
Print a synopsis of standard options. Can also use -help, -h, or -?.
javac --help
--help-extra
none
Print help on extra options. Can also use -X.
javac -X
--version
none
Print version information. Can also use -version.
javac --version
The javac compiler supports reading options from files using @filename. This is useful for long command lines:
javac @options.txt @sources.txt
where options.txt contains:
-d build
-cp lib/dep1.jar:lib/dep2.jar
-Xlint:all
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 compilation
  • 1: Compilation errors occurred
  • 2: Invalid command-line arguments
  • 3: System error or resource exhaustion
  • 4: Abnormal termination

See Also

  • java - Java application launcher
  • javadoc - API documentation generator
  • jar - JAR file tool