在IntelliJ IDEA中为JDK模块添加类的配置疑问
--patch-module in IntelliJ IDEA for JDK Module Patching Hey there! Let's clear up your confusion around using --patch-module to add a class that interacts with internal classes of a JDK module in IntelliJ. This feature can feel opaque at first, but let's break it down step by step.
First: What is --patch-module?
This JVM flag lets you "patch" an existing module (either your own custom module or a JDK built-in module) by adding or replacing classes in it at runtime (or compile time). It's perfect when you need to access internal package classes that aren't exported publicly by the module.
Figuring Out the <module> and <file> Parameters
Let's break down each part of --patch-module <module>=<file>:
1. Finding <module>: The Module Name
This is not the folder name of your module—it's the official module identifier defined in the module-info.java file at the root of your module.
- For your custom JDK-style module: Open
module-info.java, look for the line starting withmodule. For example, if it saysmodule com.example.mycustomjdk { ... }, then<module>iscom.example.mycustomjdk. - For JDK built-in modules (like
java.baseorjava.sql): Use their standard module names (you can list all JDK modules withjava --list-modulesin your terminal).
2. Finding <file>: The Path to Your Patch Class
This points to the root directory containing your compiled .class file (or source files, if using it with javac). Here's how to get it right:
- If you're using it in IntelliJ's run configuration (VM Options): Use the path to the output directory where your patch class is compiled. For example, if your class
com.example.MyPatchHelperis compiled toout/production/classes/com/example/MyPatchHelper.class, then<file>isout/production/classes. - If you're using it with
javac(compile-time patching): Use the path to your source directory (where your.javapatch file lives) to let the compiler access the module's internal APIs during compilation.
Why Do Some Examples Include javac?
Great question—this depends on whether you're using --patch-module at compile time or runtime:
- With
javac: This is for compiling your patch class. Since your class is accessing internal APIs of the target module, the compiler would normally block this. Addingjavac --patch-module <module>=<source-path> YourPatchClass.javatells the compiler to treat your class as part of the target module, allowing access to internal packages. - Without
javac: This is for runtime (what you're doing in IntelliJ's VM Options). Here, you're telling the JVM to load your pre-compiled.classfile as part of the target module when running your program.
Step-by-Step for IntelliJ IDEA
Let's put this into action with your scenario:
- Confirm your target module's name: Open its
module-info.javaand note the module identifier (e.g.,my.jdk.module). - Write and compile your patch class: Create the class that needs to interact with the module's internal classes, and let IntelliJ compile it (it usually goes to
out/production/<module-name>ortarget/classes). - Update your run configuration:
- Go to
Run > Edit Configurations. - Select the run configuration you want to modify.
- In the
VM Optionsfield, add:
(Replace--patch-module my.jdk.module=/full/path/to/your/compiled/class/directorymy.jdk.modulewith your actual module name, and the path with the root directory containing your compiled.classfiles.)
- Go to
- Run your program: The JVM will now load your patch class as part of the target module, letting it interact with internal classes freely.
Quick Note
When using --patch-module, your patch class is treated as part of the target module at runtime—you don't need to modify the target module's module-info.java to open packages (since your class is now "inside" the module).
内容的提问来源于stack exchange,提问作者Arthur

