Compilation and Execution Process
How Java code is compiled and executed
Interview Relevant: Understanding bytecode and JVM
Java Compilation and Execution Process
Understanding how Java programs are compiled and executed is crucial for debugging and performance optimization.
The Two-Step Process
Unlike languages like C/C++ that compile directly to machine code, Java follows a two-step process:
Step 1: Compilation (Source Code → Bytecode)
- Tool: javac (Java Compiler)
- Input: .java file (source code)
- Output: .class file (bytecode)
- Process:
- Lexical analysis - breaking code into tokens
- Syntax analysis - checking grammar rules
- Semantic analysis - type checking
- Bytecode generation - platform-independent instructions
Step 2: Execution (Bytecode → Machine Code)
- Tool: java (JVM)
- Input: .class file (bytecode)
- Output: Program execution
- Process:
- Class Loader loads .class files
- Bytecode Verifier ensures code safety
- Interpreter/JIT Compiler executes bytecode
- Runtime system manages memory and threads
Why Bytecode?
- Platform Independence: Same .class file runs on any platform with JVM
- Security: Bytecode can be verified before execution
- Optimization: JIT compiler can optimize at runtime
- Distribution: Can distribute .class files instead of source
JIT (Just-In-Time) Compilation
Modern JVMs use JIT compilation to improve performance:
- Initially interprets bytecode (fast startup)
- Identifies frequently executed code ("hot spots")
- Compiles hot spots to native machine code
- Caches compiled code for reuse
- Results in performance approaching native code
Comparison with Other Languages
| Language | Compilation | Execution |
|---|---|---|
| C/C++ | Direct to machine code | Platform-specific executable |
| Java | To bytecode | JVM interprets/JIT compiles |
| Python | To bytecode (implicit) | Interpreter executes bytecode |
Code Examples
Commands for compiling and executing Java programs
java
1# Compile Java source file
2javac HelloWorld.java
3# This creates HelloWorld.class
4
5# Run the compiled class
6java HelloWorld
7# Note: Don't include .class extension
8
9# Compile with verbose output
10javac -verbose HelloWorld.java
11
12# View bytecode (requires javap tool)
13javap -c HelloWorldAdvanced compilation and execution commands
java
1# Compile multiple files
2javac File1.java File2.java File3.java
3
4# Compile all .java files in current directory
5javac *.java
6
7# Compile with specific output directory
8javac -d bin src/HelloWorld.java
9
10# Run class from specific directory
11java -cp bin HelloWorldUse Cases
- Understanding platform independence (WORA)
- Debugging compilation errors
- Performance optimization
- Build process configuration
- Understanding ClassLoader and class loading mechanism
Common Mistakes to Avoid
- Trying to run .java file instead of .class file
- Including .class extension when running: java HelloWorld.class (wrong)
- Not understanding classpath and package structure
- Expecting instant performance - JIT needs warmup time
- Not realizing bytecode can be decompiled (security concern)