> For the complete documentation index, see [llms.txt](https://petercheng7788.gitbook.io/developer-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://petercheng7788.gitbook.io/developer-note/programming-language/java-concept/compilation-and-execution.md).

# Compilation and Execution

## Introduction

![](/files/-MJ18QwGCQVoq7v2jLUW)

* 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&#x20;
* Run time Error is mainly related to out of memory,  divided by 0 , dereferencing a null pointer

![JVM Memory](/files/-MJ1Mtl51xKHVq0Ep6EJ)

* 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 &#x20;

1. **JDK**(Java Development Kit) : JDK is intended for software developers and includes development tools such as the Java compiler, Javadoc, Jar, and a debugger.
2. **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.
3. **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.&#x20;

## 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&#x20;
* After execution of the method, the corresponding stack frame will be flushed
* Last in First Out

![](/files/-MVXAPZ3m52bcVTL9CxL)

![](/files/-MVXAWhdb4_8U1Kv_qK_)

### 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.MF` file with metadata (can define the Main-Class to make it executable)

{% code title="MANIFEST.MF" %}

```
Manifest-Version: 1.0
Main-Class: org.springframework.boot.loader.launch.JarLauncher
Start-Class: com.example.java_playground.JavaPlaygroundApplication
Spring-Boot-Version: 4.0.2
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Spring-Boot-Layers-Index: BOOT-INF/layers.idx
Build-Jdk-Spec: 21
Implementation-Title: java-playground
Implementation-Version: 1.0.1-SNAPSHOT
```

{% endcode %}

#### 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

```docker
FROM openjdk:21-ea-21-jdk-slim
ARG JAR_FILE=build/libs/*.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-Dspring.profiles.active=local", "-jar", "/app.jar"]
EXPOSE 8080
```
