Skip to content
Latchkey

javac Command: Compile Java in CI

javac compiles Java source files (.java) into bytecode class files (.class).

javac is the reference Java compiler shipped with the JDK. Maven and Gradle invoke it under the hood, but it appears directly in lightweight CI scripts and Docker images. Its flags control output location, classpath, and target bytecode version.

Common flags

  • -d DIR - write generated .class files to DIR
  • -cp / -classpath PATH - set the user class path for dependencies
  • --release 17 - compile against a specific Java release (preferred over -source/-target)
  • -source 17 / -target 17 - set source and bytecode versions
  • -Xlint:all - enable all recommended warnings
  • -Werror - terminate compilation if any warnings occur
  • @argfile - read options and file names from a file (avoids long command lines)

Example in CI

Compile all sources to a build directory with a pinned release and strict warnings:

shell
javac --release 17 -Xlint:all -Werror -d build/classes -cp "libs/*" $(find src -name "*.java")

Common errors in CI

  • error: package com.example does not exist - missing dependency on the classpath
  • error: cannot find symbol - typo, missing import, or unbuilt dependency
  • error: invalid target release: 21 - JDK is older than the requested release
  • error: class file has wrong version 61.0, should be 52.0 - bytecode/JDK mismatch

Key takeaways

  • -d controls output location; -cp wires in dependencies.
  • Prefer --release N over -source/-target to avoid bootclasspath mismatches.
  • Most CI failures are missing-classpath "package does not exist" / "cannot find symbol".

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →