You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在IntelliJ IDEA中为JDK模块添加类的配置疑问

How to Use --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 with module. For example, if it says module com.example.mycustomjdk { ... }, then <module> is com.example.mycustomjdk.
  • For JDK built-in modules (like java.base or java.sql): Use their standard module names (you can list all JDK modules with java --list-modules in 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.MyPatchHelper is compiled to out/production/classes/com/example/MyPatchHelper.class, then <file> is out/production/classes.
  • If you're using it with javac (compile-time patching): Use the path to your source directory (where your .java patch 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. Adding javac --patch-module <module>=<source-path> YourPatchClass.java tells 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 .class file 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:

  1. Confirm your target module's name: Open its module-info.java and note the module identifier (e.g., my.jdk.module).
  2. 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> or target/classes).
  3. Update your run configuration:
    • Go to Run > Edit Configurations.
    • Select the run configuration you want to modify.
    • In the VM Options field, add:
      --patch-module my.jdk.module=/full/path/to/your/compiled/class/directory
      
      (Replace my.jdk.module with your actual module name, and the path with the root directory containing your compiled .class files.)
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:20:41