Compilation and Execution
Introduction

During Compilation (Compile time), the compiler convert source code (.java) into bytecode (.class)
Compile Time Error is mainly related to syntax error of source code
During Execution (Run time), the class file will read ,the related class will be loaded and the relevant information (e.g: static variable, method ) will be stored into memory of JVM
Run time Error is mainly related to out of memory, divided by 0 , dereferencing a null pointer

Finally , the class file will be read line by line , data in memory area , convert into native machine code and be executed by execution engine
JDK(Java Development Kit) : JDK is intended for software developers and includes development tools such as the Java compiler, Javadoc, Jar, and a debugger.
JRE(Java Runtime Environment) : JRE contains the parts of the Java libraries required to run Java programs and is intended for end users. JRE can be view as a subset of JDK.
JVM: JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed. JVMs are available for many hardware and software platforms.
Heap and Stack
Heap
Used to store the address of the object such as String, ...
If heaps is fulled, Out of memory error will be thrown
Stack
When the method is executed, a new stack will be created and the local primitive variable and the reference of the object which can be found in heaps will be stored
After execution of the method, the corresponding stack frame will be flushed
Last in First Out


Garbage Collection
In order to prevent out of memory, as there are many unused objects, the heap memory will be cleared automatically in the background
Jar (Java ARchive)
The java application artifact
What it is: A packaged collection of:
Compiled .class files
Resources (properties, images, etc.)
A
MANIFEST.MFfile with metadata (can define the Main-Class to make it executable)
Step‑by‑step behind the scenes
1. OS starts the Java launcher
Your shell runs the java executable (from your JDK/JRE).
The Java launcher process starts.
2. Launcher sees -jar my-app.jar
-jar means: “ignore normal -cp / CLASSPATH, use this JAR to find the entry point and classpath.”
It opens my-app.jar like a ZIP file.
3. Reads the JAR manifest
Inside the JAR, it looks for: META-INF/MANIFEST.MF.
It reads entries such as:
Main-Class: com.example.Main → which class has public static void main(String[] args)
Class-Path: lib/a.jar lib/b.jar (optional extra jars)
If Main-Class is missing, java -jar fails with “no main manifest attribute”.
4. Builds the classpath and classloader
Uses the JAR itself + anything from the manifest Class-Path as the application classpath.
Creates a classloader that knows how to load classes and resources from those JARs.
5. Starts the JVM
Initializes the JVM runtime:
Sets up heap, stacks, garbage collector, JIT compiler, etc.
Loads core Java libraries (java.base, etc.).
6. Loads your main class
Using the app classloader, it loads the class from Main-Class, e.g. com.example.Main.
Verifies the bytecode (safety checks) before running it.
Docker
Last updated