Overview
javac compiles .java sources to .class bytecode, one class file per top-level (and
nested) type. It performs no meaningful optimisation — that is the JIT's job at runtime —
so the interesting flags are about where it resolves types, which platform version it
targets, and what it warns about. In real projects it is almost always driven by Maven
or Gradle rather than invoked by hand.
Key points
- Compilation model: resolves types against the classpath (
-cp) and/or module path (-p/--module-path); an on-demand implicit compilation pulls in sources it finds via-sourcepath. Modules get resolved permodule-info.java. --release N: compiles and links against the Java N platform API — the safe way to target older versions. Bare-source/-targetset language level and bytecode version but link against the current JDK's API, allowing accidental use of newer APIs.- Annotation processing: processors on
--processor-pathrun in rounds before code generation (Lombok, MapStruct, Micronaut/Quarkus build-time DI).-proc:onlyruns processors without compiling;-proc:nonedisables them (the default became no-implicit-processing in recent JDKs — declare processors explicitly). - Lint:
-Xlint:all(or targeted keys likeunchecked,deprecation,this-escape) plus-Werrorto fail the build; the pragmatic baseline for new code. - Preview features:
--enable-preview(with matching--release) marks class files as preview, and the launcher must enable preview too. - Build-tool driving: Maven's
maven-compiler-pluginand Gradle'sJavaCompiletask set release level, processor paths and incremental compilation; you configure those rather than raw flags.
Examples
# Compile a small tree against Java 21 APIs, warnings as errors
javac --release 21 -Xlint:all -Werror -d out $(find src -name '*.java')
# Modular compilation
javac -p libs -d out --module-source-path src $(find src -name '*.java')
Related
- The JDK, the JRE & their tools — parent catalog of the toolchain.
- java launcher — runs what javac produces; shares the preview-feature handshake.
- javap — verify what javac actually generated.
- Build ecosystem — Maven/Gradle own javac invocation in practice.