Java11模块中--describe-module失效时,如何使用第三方库jlfgr?
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.jarto something likejlfgr.jarorjlfgr-v1_0.jar.- If you use
jlfgr.jar, the automatic module name becomesjlfgr. - If you use
jlfgr-v1_0.jar, it becomesjlfgr.v1.0(valid becausev1starts with a letter).
- If you use
- Update your
module-info.javato match the new module name. For example, if you renamed tojlfgr.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:
- Create a simple
module-info.javafor the library. First check the jar's internal structure to find which packages it exports (for jlfgr, it's likelyjavax.swing.plaf.metaland related packages):module javax.jlfgr { exports javax.swing.plaf.metal; // Add any other exported packages from the jar here } - Compile this module descriptor, patching it onto the existing jar:
javac --patch-module javax.jlfgr=jlfgr-1_0.jar module-info.java - Add the compiled
module-info.classto the jar:jar uf jlfgr-1_0.jar -C . module-info.class - Now your original
module-info.java(withrequires javax.jlfgr;) will work perfectly, and running--describe-modulewill output valid info forjavax.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

