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:
    1. Lexical analysis - breaking code into tokens
    2. Syntax analysis - checking grammar rules
    3. Semantic analysis - type checking
    4. Bytecode generation - platform-independent instructions

Step 2: Execution (Bytecode → Machine Code)

  • Tool: java (JVM)
  • Input: .class file (bytecode)
  • Output: Program execution
  • Process:
    1. Class Loader loads .class files
    2. Bytecode Verifier ensures code safety
    3. Interpreter/JIT Compiler executes bytecode
    4. 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 HelloWorld

Advanced 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 HelloWorld

Use 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)