> For the complete documentation index, see [llms.txt](https://duc193.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://duc193.gitbook.io/notes/web-security/java-deserialize/class-loader.md).

# Class Loader

Trước khi chương trình Java chạy, code sẽ được compile thành bytecode (file `.class`). Khi 1 class được yêu cầu, `ClassLoader` sẽ tìm và nạp `bytecode` của `class` đó. Sau đó `JVM` dùng các hàm native (defineClass0/1/2) để chuyển `bytecode` thành đối tượng Class trong memory.

Trước khi chạy code java, các Class phải được JVM load trước. `ClassLoader` có chức năng chính là load các file Class Java. Các class loader JVM bao gồm : `Bootstrap ClassLoader`, `Extension ClassLoader`, `App ClassLoader`, `AppClassLoader` là class loader mặc định nếu chúng ta không chỉ định class loader.

Khi một `ClassLoader` nhận yêu cầu nạp một class, nó sẽ không thực hiện ngay việc nạp class đó, mà sẽ ủy quyền việc nạp cho `ClassLoader` cha (parent). Quá trình này tiếp tục theo chuỗi `cha-con` cho đến khi đến `BootstrapClassLoader`. Nếu không `ClassLoader` nào trong chuỗi tìm được class, thì loader gốc (gọi `loadClass`) mới tự thực hiện việc nạp class.&#x20;

<figure><img src="https://hackmd.io/_uploads/SJOpMwHGWl.png" alt=""><figcaption></figcaption></figure>

`ClassLoader` có các phương thức chính sau :

* `loadClass` : Load Class
* `findClass` : Tìm Class
* `findLoadedClass` : Tìm Class đã được MVN load
* `defineClass`: biến một mảng `byte[]` (class file) thành một class `Class<?>` có thể dùng trong JVM.
* `resolveClass`: liên kết các class đã được nạp vào.

## URLClassLoader

`URLClassLoader` là một class con của `ClassLoader` thuộc package `java.net`, cho phép chúng ta:

* Load class từ folder cụ thể (`file://`)
* Load class từ file `JAR`
* Load class từ server `HTTP/Tomcat`

### Load Class từ file&#x20;

Chúng ta có thể LoadClass từ folder cụ thể. Demo tạo 1 file class trong `com.test` sẽ print ra "Load Thành Công" và mở `calc` lên:&#x20;

<figure><img src="https://hackmd.io/_uploads/rk22YvBGZe.png" alt=""><figcaption></figcaption></figure>

Code file `LocalClassLoader.class`:

<figure><img src="https://hackmd.io/_uploads/Hypq9DrfZx.png" alt=""><figcaption></figcaption></figure>

nó sẽ load file `Test.class` ở trong `classess/com/test` sau đó `newInstance()` sẽ new Class Test. Khi đó function construct `Test()` sẽ chạy và mở calc lên. Cấu trúc thư mục sẽ như sau :&#x20;

<figure><img src="https://hackmd.io/_uploads/S1PTqPBMZe.png" alt=""><figcaption></figcaption></figure>

Ban đầu, chúng ta sẽ beine dịch `javac` `Test.java` vào trong `classess.com.test` và `LocalClassLoader`

&#x20;

<figure><img src="https://hackmd.io/_uploads/HyufiPBG-x.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://hackmd.io/_uploads/Byd7svSfZe.png" alt=""><figcaption></figcaption></figure>

Sau đó run `java LocalClassLoader`

&#x20;&#x20;

<figure><img src="https://hackmd.io/_uploads/BJMBsvHzbx.png" alt=""><figcaption></figcaption></figure>

Vậy là đã load class từ file thành công.

### From HTTP

Trước tiên thì tạo file `Test.java` từ vps

<figure><img src="https://hackmd.io/_uploads/rkGOnwHMbx.png" alt=""><figcaption></figcaption></figure>

build file `.class` và mở port

<figure><img src="https://hackmd.io/_uploads/r16GpwrfWe.png" alt=""><figcaption></figcaption></figure>

Build file `HttpClassLoader`:

<figure><img src="https://hackmd.io/_uploads/Skl_CwBz-e.png" alt=""><figcaption></figcaption></figure>

run :

<figure><img src="https://hackmd.io/_uploads/r1ptADrGZx.png" alt=""><figcaption></figcaption></figure>

Vậy là đã load thành công. log sv:&#x20;

<figure><img src="https://hackmd.io/_uploads/Sy2c0DHzWl.png" alt=""><figcaption></figcaption></figure>

## Reference:

[https://github.com/phith0n/JavaThings ](https://github.com/phith0n/JavaThings)

<https://www.javasec.org/><https://blog.csdn.net/qq_52988816/article/details/126396161><https://blog.csdn.net/2401_87299237/article/details/142433301><https://blog.csdn.net/weixin_38233104/article/details/131491994><https://blog.csdn.net/hc199854/article/details/107285579>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://duc193.gitbook.io/notes/web-security/java-deserialize/class-loader.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
