From 5170d5051794142444bcbdf3dd7ac4a92c0b95f7 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Thu, 15 Oct 2026 20:02:32 +0800 Subject: [PATCH] Docs: update config docs to v2 schema and add config.md - docs: add config.md documenting the v2 schema ([JuiceAgent] / [JarLoader]), key defaults and the BuiltIn / CustomJar modes - docs: rewrite advanced.md around the JarLoader.Mode = CustomJar workflow instead of the removed JUICEAGENT_API_* environment variables - readme: migrate the Quick Start config example from v1 to v2 and link docs/config.md, list inject-via-java-api under Examples - examples: update inject-via-java-api HowToUse docs (.md/.txt, EN/ZH) to the v2 config --- README.md | 17 +++--- README_ZH.md | 17 +++--- docs/advanced.md | 40 ++++++++------ docs/config.md | 65 +++++++++++++++++++++++ examples/inject-via-java-api/HowToUse.md | 9 ++-- examples/inject-via-java-api/HowToUse.txt | 9 ++-- examples/inject-via-java-api/如何使用.md | 9 ++-- examples/inject-via-java-api/如何使用.txt | 9 ++-- 8 files changed, 118 insertions(+), 57 deletions(-) create mode 100644 docs/config.md diff --git a/README.md b/README.md index c88bc30..3eebe4d 100644 --- a/README.md +++ b/README.md @@ -120,17 +120,15 @@ Extract the ZIP file to a directory: ```toml [JuiceAgent] -Version = 1 - -[JuiceAgent.Loader] -# Path to libagent.dll, default is "./libagent.dll" +Version = 2 +# Path to libagent.dll, default is "./libagent.dll" (relative to the runtime directory) JuiceAgentNativeLibraryPath = "" -[JuiceAgent.Modules] - -[JuiceAgent.Modules.JarLoader] +[JarLoader] Enabled = true -# Directory containing dependency JARs to load first +# Mode: BuiltIn | CustomJar +Mode = "BuiltIn" +# Directory containing dependency JARs to load first (BuiltIn mode) InjectionDir = "./injection" # Path to the JAR file to be loaded JarPath = "./MyCustomJar.jar" @@ -140,7 +138,7 @@ EntryClass = "Example.Main" EntryMethod = "run" ``` -Save the file as `config.toml` in the same directory (the runtime directory). Relative paths are resolved against the runtime directory. +Save the file as `config.toml` in the same directory (the runtime directory). Relative paths are resolved against the runtime directory. See [Configuration](docs/config.md) for the complete reference. - YourDir - **config.toml** @@ -196,6 +194,7 @@ The following native functions are exposed to Java through the `cn.xiaozhou233.j ## Examples +- `examples/inject-via-java-api` — injects JuiceAgent into a running JVM through the Java API (JNI), without needing `injector.exe`. - `examples/load-via-injector-exe` — loads a JAR into a target JVM via `injector.exe` and runs an entry point. - `examples/retransform-class` — demonstrates class retransformation with a target JVM and a modified copy of the class. diff --git a/README_ZH.md b/README_ZH.md index 3d098bd..4c677b6 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -121,17 +121,15 @@ JuiceAgent 是 [JuiceAgent-API](https://github.com/xiaozhou233/JuiceAgent-API) ```toml [JuiceAgent] -Version = 1 - -[JuiceAgent.Loader] -# libagent.dll 的路径,默认是 "./libagent.dll" +Version = 2 +# libagent.dll 的路径,默认是 "./libagent.dll"(相对于运行时目录) JuiceAgentNativeLibraryPath = "" -[JuiceAgent.Modules] - -[JuiceAgent.Modules.JarLoader] +[JarLoader] Enabled = true -# 需要优先加载的依赖 JAR 所在目录 +# Mode:BuiltIn | CustomJar +Mode = "BuiltIn" +# 需要优先加载的依赖 JAR 所在目录(BuiltIn 模式) InjectionDir = "./injection" # 要加载的 JAR 文件路径 JarPath = "./MyCustomJar.jar" @@ -141,7 +139,7 @@ EntryClass = "Example.Main" EntryMethod = "run" ``` -将文件保存为 `config.toml` 并放在同一目录(运行时目录)中。相对路径以运行时目录为基准解析。 +将文件保存为 `config.toml` 并放在同一目录(运行时目录)中。相对路径以运行时目录为基准解析。完整配置说明见 [Configuration](docs/config.md)。 - YourDir - **config.toml** @@ -197,6 +195,7 @@ Input PID: ## 示例 +- `examples/inject-via-java-api` — 通过 Java API(JNI)将 JuiceAgent 注入到正在运行的 JVM,无需 `injector.exe`。 - `examples/load-via-injector-exe` — 通过 `injector.exe` 将 JAR 加载到目标 JVM 并运行入口点。 - `examples/retransform-class` — 演示使用目标 JVM 和修改后的类副本进行类重变换。 diff --git a/docs/advanced.md b/docs/advanced.md index 5c9f9c1..a9f492e 100644 --- a/docs/advanced.md +++ b/docs/advanced.md @@ -1,24 +1,34 @@ # Advanced ## Load Custom API / Directly Load Jar -WARN: This feature didn't implement yet. -### Describe -JuiceAgent loads custom JARs through the following workflow: +JuiceAgent runs in **BuiltIn** mode by default: `libloader` registers the embedded +JuiceAgent-API jar, the agent bootstraps it, and the JarLoader module loads your JAR +and invokes its entry method. -1. Inject `libloader` into the target process. -2. Load **JuiceAgent-API** and execute its initialization entry point. -3. Invoke the `JarLoader` from **JuiceAgent-API** to load the specified JAR into the JVM. +If you don't need JuiceAgent-API — for example you want to provide your own bootstrap +and call an arbitrary class/method — set `JarLoader.Mode` to **`CustomJar`**: -The second step is fully customizable. You can replace **JuiceAgent-API** with your own JAR and call any class method you specify. +```toml +[JuiceAgent] +Version = 2 -### Configuration +[JarLoader] +Mode = "CustomJar" +JarPath = "./my-agent.jar" +EntryClass = "com.example.MyBootstrap" +EntryMethod = "start" +``` -Set the following environment variables before launching `libloader`: +Workflow in `CustomJar` mode: -```bat -set JUICEAGENT_API_ENABLED=true -set JUICEAGENT_API_JAR= -set JUICEAGENT_API_CLASS= -set JUICEAGENT_API_METHOD= -``` \ No newline at end of file +1. `libloader` attaches to the target JVM and reads `config.toml`. +2. It adds `JarPath` to the system class loader search path. JuiceAgent-API is **not** loaded. +3. It invokes `EntryClass.EntryMethod`. + +`EntryMethod` must be a `public static void` method that takes no arguments. + +> `JarLoader.Enabled` and `JarLoader.InjectionDir` only apply to `BuiltIn` mode; they are +> ignored in `CustomJar` mode. + +See [Configuration](config.md) for the complete list of keys. diff --git a/docs/config.md b/docs/config.md new file mode 100644 index 0000000..1422ad5 --- /dev/null +++ b/docs/config.md @@ -0,0 +1,65 @@ +# Configuration (`config.toml`) + +JuiceAgent reads a single `config.toml` from the **runtime directory** (the directory +passed to the injector / loader; the current working directory is used when none is +given). Paths that start with `./` are resolved against the runtime directory; +absolute paths are used as-is. + +The current schema version is **2**. `JuiceAgent.Version` must match the version +expected by the binaries, otherwise the loader stops with a version mismatch error. + +## Example + +```toml +[JuiceAgent] +Version = 2 +JuiceAgentNativeLibraryPath = "" + +[JarLoader] +Enabled = true +Mode = "BuiltIn" +InjectionDir = "./injection" +JarPath = "./MyCustomJar.jar" +EntryClass = "Example.Main" +EntryMethod = "run" +``` + +## `[JuiceAgent]` + +| Key | Type | Required | Default | Description | +| --- | --- | --- | --- | --- | +| `Version` | integer | yes | — | Configuration schema version. Must be `2`. | +| `JuiceAgentNativeLibraryPath` | string (path) | no | `./libagent.dll` | Path to `libagent.dll`, loaded by the bootstrap after injection. | + +## `[JarLoader]` + +| Key | Type | Required | Default | Description | +| --- | --- | --- | --- | --- | +| `Mode` | string | yes | — | How the target JAR is registered: `BuiltIn` or `CustomJar`. | +| `Enabled` | boolean | no | `false` | Run the JarLoader module after the agent is initialized (`BuiltIn` mode only). When `false`, no JAR is loaded and no entry method is invoked. | +| `InjectionDir` | string (path) | no | `./injection` | Directory of dependency JARs loaded before the main JAR (`BuiltIn` mode). | +| `JarPath` | string (path) | see below | `./Entry.jar` (`BuiltIn`) | The JAR to load. | +| `EntryClass` | string | see below | `Example.Main` (`BuiltIn`) | Entry class to invoke after loading the JAR. | +| `EntryMethod` | string | see below | `start` (`BuiltIn`) | Entry method, must be a `public static void` method with no parameters. | + +> In `CustomJar` mode, `JarPath`, `EntryClass` and `EntryMethod` are required (there are +> no defaults); `Enabled` and `InjectionDir` are not used. + +## Modes + +### `BuiltIn` (recommended) + +1. `libloader` extracts the embedded **JuiceAgent-API** jar and adds it to the system class loader. +2. The agent bootstraps JuiceAgent-API (`JuiceAgentBootstrap.start`). +3. If `Enabled = true`, the JarLoader module loads every `*.jar` from `InjectionDir`, then loads `JarPath`, and invokes `EntryClass.EntryMethod`. + +This is the mode used by most users: keep JuiceAgent-API and just supply the JAR and +entry point to run. + +### `CustomJar` + +1. `libloader` adds `JarPath` itself to the system class loader. **JuiceAgent-API is not used.** +2. It invokes `EntryClass.EntryMethod` directly. + +Use this when you want to bring your own bootstrap and call an arbitrary class/method. +See [Advanced](advanced.md#load-custom-api--directly-load-jar) for details. diff --git a/examples/inject-via-java-api/HowToUse.md b/examples/inject-via-java-api/HowToUse.md index 824eb16..7c4814e 100644 --- a/examples/inject-via-java-api/HowToUse.md +++ b/examples/inject-via-java-api/HowToUse.md @@ -64,15 +64,12 @@ in `config.toml` and execute its entry method. ```toml [JuiceAgent] -Version = 1 - -[JuiceAgent.Loader] +Version = 2 JuiceAgentNativeLibraryPath = "" -[JuiceAgent.Modules] - -[JuiceAgent.Modules.JarLoader] +[JarLoader] Enabled = true +Mode = "BuiltIn" InjectionDir = "./injection" JarPath = "./demo.jar" EntryClass = "Example.Main" diff --git a/examples/inject-via-java-api/HowToUse.txt b/examples/inject-via-java-api/HowToUse.txt index 9a8246e..40e19c5 100644 --- a/examples/inject-via-java-api/HowToUse.txt +++ b/examples/inject-via-java-api/HowToUse.txt @@ -47,15 +47,12 @@ Configuration (config.toml) ============================ [JuiceAgent] - Version = 1 - - [JuiceAgent.Loader] + Version = 2 JuiceAgentNativeLibraryPath = "" - [JuiceAgent.Modules] - - [JuiceAgent.Modules.JarLoader] + [JarLoader] Enabled = true + Mode = "BuiltIn" InjectionDir = "./injection" JarPath = "./demo.jar" EntryClass = "Example.Main" diff --git a/examples/inject-via-java-api/如何使用.md b/examples/inject-via-java-api/如何使用.md index 87c3f75..50fa691 100644 --- a/examples/inject-via-java-api/如何使用.md +++ b/examples/inject-via-java-api/如何使用.md @@ -63,15 +63,12 @@ ```toml [JuiceAgent] -Version = 1 - -[JuiceAgent.Loader] +Version = 2 JuiceAgentNativeLibraryPath = "" -[JuiceAgent.Modules] - -[JuiceAgent.Modules.JarLoader] +[JarLoader] Enabled = true +Mode = "BuiltIn" InjectionDir = "./injection" JarPath = "./demo.jar" EntryClass = "Example.Main" diff --git a/examples/inject-via-java-api/如何使用.txt b/examples/inject-via-java-api/如何使用.txt index e1b3066..108d46e 100644 --- a/examples/inject-via-java-api/如何使用.txt +++ b/examples/inject-via-java-api/如何使用.txt @@ -46,15 +46,12 @@ ======================= [JuiceAgent] - Version = 1 - - [JuiceAgent.Loader] + Version = 2 JuiceAgentNativeLibraryPath = "" - [JuiceAgent.Modules] - - [JuiceAgent.Modules.JarLoader] + [JarLoader] Enabled = true + Mode = "BuiltIn" InjectionDir = "./injection" JarPath = "./demo.jar" EntryClass = "Example.Main"