EMZETT.
Login

javac (Java compiler)

In short: javac is the compiler of the JDK. It translates Java source (.java) into bytecode (.class) that the JVM can run.

In more detail: The compiler checks syntax and types and reports errors before the program runs at all. It produces no machine code for the processor but platform-independent bytecode.

In Depth

javac Hello.java          # creates Hello.class
java Hello                # starts the JVM with the class
javac -d out src/*.java   # specify the output folder
javac --release 17 Hello.java   # produce bytecode for Java 17
javap -c Hello            # show bytecode readably (disassembler)

The path from file to running program:

  1. Source Hello.java is written.
  2. javac parses, checks types and names, optimizes lightly and writes Hello.class.
  3. java starts the JVM; the class loader loads the .class files.
  4. The JVM interprets the bytecode and translates frequently running parts into machine code via the JIT compiler.
  • Compiler errors: Mostly “cannot find symbol” (misspelled name, missing import), “incompatible types”, “missing return statement”.
  • Warnings: -Xlint:all shows additional hints, for example on deprecated APIs.
  • Several files: The compiler finds dependent classes via the classpath (-cp) or module path.
  • Practice: Usually Maven, Gradle or the IDE call javac.
  • Annotation processing: javac runs annotation processors that, for example, generate code (Lombok, MapStruct).
  • Single-file mode: Since Java 11, java Hello.java launches a single source file directly; the compiler runs in memory.

See also: Bytecode, Compiler, JIT compiler (just-in-time)