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:
- Source
Hello.javais written. javacparses, checks types and names, optimizes lightly and writesHello.class.javastarts the JVM; the class loader loads the.classfiles.- 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:allshows 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:
javacruns annotation processors that, for example, generate code (Lombok, MapStruct). - Single-file mode: Since Java 11,
java Hello.javalaunches a single source file directly; the compiler runs in memory.
See also: Bytecode, Compiler, JIT compiler (just-in-time)