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

Java11模块中--describe-module失效时,如何使用第三方库jlfgr?

Fixing Invalid Module Name Error with jlfgr-1_0.jar in Java 11 + JavaFX 11

Let's break down what's going on here and walk through actionable fixes:

First, the root cause: Your jlfgr-1_0.jar is an automatic module (it lacks a built-in module-info.class). When the JVM tries to generate a module name for it, it converts non-alphanumeric characters in the filename to dots. So jlfgr-1_0.jar becomes jlfgr.1.0—and that's invalid because module name segments can't start with a number (they need to be valid Java identifiers).

Here are three solid solutions to get this working with your modular JavaFX project:

Solution 1: Rename the Jar for a Valid Automatic Module Name

This is the quickest fix, no jar modification required:

  • Rename jlfgr-1_0.jar to something like jlfgr.jar or jlfgr-v1_0.jar.
    • If you use jlfgr.jar, the automatic module name becomes jlfgr.
    • If you use jlfgr-v1_0.jar, it becomes jlfgr.v1.0 (valid because v1 starts with a letter).
  • Update your module-info.java to match the new module name. For example, if you renamed to jlfgr.jar:
    module FXTodo {
        requires javafx.base;
        requires javafx.controls;
        requires javafx.graphics;
        requires javafx.fxml;
        requires jlfgr; // Updated from javax.jlfgr
        opens com.something.todolist;
    }
    
  • Verify it works by running:
    java --module-path path/to/renamed/jar --describe-module jlfgr
    

Pros: Super fast, no complex steps.
Cons: If you use a dependency manager (like Maven/Gradle) that auto-downloads the jar, you'll need to configure it to rename the jar on each update.

Solution 2: Add a Valid Module Descriptor to the Jar

This is the most robust, long-term fix—you'll turn the automatic module into an explicit module:

  1. Create a simple module-info.java for the library. First check the jar's internal structure to find which packages it exports (for jlfgr, it's likely javax.swing.plaf.metal and related packages):
    module javax.jlfgr {
        exports javax.swing.plaf.metal;
        // Add any other exported packages from the jar here
    }
    
  2. Compile this module descriptor, patching it onto the existing jar:
    javac --patch-module javax.jlfgr=jlfgr-1_0.jar module-info.java
    
  3. Add the compiled module-info.class to the jar:
    jar uf jlfgr-1_0.jar -C . module-info.class
    
  4. Now your original module-info.java (with requires javax.jlfgr;) will work perfectly, and running --describe-module will output valid info for javax.jlfgr.

Pros: Creates a proper explicit module, no need to rename the jar, works with dependency managers if you host the modified jar.
Cons: Requires a bit of work to identify all exported packages.

Solution 3: Manual Module Name Override (Temporary Fix)

If you need a quick test without modifying files, you can manually specify the module name at compile/runtime:

  • When compiling your project:
    javac --module-path path/to/jlfgr-1_0.jar:path/to/javafx/libs \
          --add-modules javax.jlfgr \
          --module-source-path src \
          -d out \
          src/module-info.java src/com/something/todolist/**/*.java
    
  • When running your project:
    java --module-path out:path/to/jlfgr-1_0.jar:path/to/javafx/libs \
         --add-modules javax.jlfgr \
         --module FXTodo/com.something.todolist.Main
    

Pros: No file changes, great for quick testing.
Cons: You have to add these flags every time you compile/run—annoying for regular use.

Pick the solution that fits your workflow best! All of them will resolve the invalid module name error and let you use jlfgr in your modular JavaFX 11 project.

内容的提问来源于stack exchange,提问作者chrips

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 04:18:33