rgoussu@goussu: ~/library/java/jdk-and-tools
~/library/java/jdk-and-tools cat javac.md

javac — the Java compiler

# The JDK compiler — compilation model, classpath vs module path, --release, annotation processing, and lint flags.

Conceptsaved 2026-08-09 #java#jdk#tooling#compilation

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 per module-info.java.
  • --release N: compiles and links against the Java N platform API — the safe way to target older versions. Bare -source/-target set language level and bytecode version but link against the current JDK's API, allowing accidental use of newer APIs.
  • Annotation processing: processors on --processor-path run in rounds before code generation (Lombok, MapStruct, Micronaut/Quarkus build-time DI). -proc:only runs processors without compiling; -proc:none disables them (the default became no-implicit-processing in recent JDKs — declare processors explicitly).
  • Lint: -Xlint:all (or targeted keys like unchecked, deprecation, this-escape) plus -Werror to 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-plugin and Gradle's JavaCompile task 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