javac — Java compiler
Development / Build / javac.html
Compiles Java sources into JVM bytecode (.class files).
Basic Information
| Command Name | javac |
|---|---|
| One-line Introduction | Compiles Java sources into JVM bytecode (.class files). |
| Provided by | OpenJDK |
| Official Documentation | https://docs.oracle.com/en/java/javase/21/docs/specs/man/javac.html |
| Supported Operating Systems | Linux, macOS, Windows |
| Installation Status | Requires a JDK |
| Category | Build and compile (tags: Language toolchain (tags: javac, java, compile, jdk)) |
What is this command?
javac converts .java files into .class bytecode executed by the JVM. The output is platform independent, but running it requires a JRE/JDK of the same major version or newer.
Directory layout must match package declarations. Using -d creates package directories automatically. When files reference each other, pass them together.
What problems does it solve?
- Compiling Java sources into bytecode
- Keeping package structure while separating output
- Targeting a specific release for compatibility
- Compiling several sources at once
Key features
Bytecode output
Compiles sources into .class files.
- When to use: Testing a simple class
- What you get: You get output the JVM can run.
- Limitations: Execution is a separate step with java.
Output directory
The -d option writes into package directories.
- When to use: Building projects that use packages
- What you get: Classpath setup becomes simpler.
- Limitations: Stale output can cause confusion.
Target release
The --release option pins API and bytecode version.
- When to use: When compatibility with older JVMs is required
- What you get: Prevents accidental use of newer APIs.
- Limitations: APIs missing from that release become errors.
Installation and usage
Check whether it is installed
javac -version
sudo apt install openjdk-21-jdk-headless # Debian/Ubuntu sudo dnf install java-21-openjdk-devel # Fedora
Real-world examples
Compile one file
Produce a class file.
javac Hello.java
java Hello
Packaged project
Specify an output directory to create the package layout.
javac -d out $(find src -name '*.java')
java -cp out com.example.Main
Pin the release
Target an older release for compatibility.
javac --release 17 -d out src/Main.java