Problem
When building a custom Java runtime image with jlink, the following error occurs:
Error: automatic module cannot be used with jlink: commons.math3
or similar messages for other libraries after adding new dependencies.
This problem may reappear multiple times as jlink identifies additional automatic modules in the project.
Cause
jlink only supports explicit modules, JARs that contain a valid module-info.class descriptor.
However, many third-party libraries such as Apache Commons Math3 are automatic modules.
Automatic modules are derived from non-modular JARs that don’t have module-info.java. When jlink encounters one, it fails because it cannot determine explicit module relationships.
Example:
commons-math3-3.6.1.jar → automatic module: commons.math3
Even if you temporarily fix one dependency, new errors can appear as jlink discovers more automatic modules in the dependency graph.
Resolution
Option 1: Use a JDK with JavaFX included
Instead of modularizing every dependency manually, use a JDK that includes JavaFX and supports packaging tools out-of-the-box.
Recommended: BellSoft Liberica Full JDK version 11 or later.
Steps:
- Install BellSoft Liberica Full JDK. Download it here.
- In IntelliJ IDEA, set the project SDK to Liberica Full JDK.
- Rebuild your application.
- Use
jpackageto create the native image instead ofjlink.
This resolves most deployment errors and simplifies packaging, as JavaFX modules are already integrated.
Option 2: Convert automatic modules to explicit modules (advanced)
If you must use jlink:
-
Identify all automatic modules:
java --list-modules -
Replace or rebuild those libraries with modular equivalents.
- Example: build a modularized version of
commons-math3or switch to a library that includesmodule-info.java.
- Example: build a modularized version of
-
Update your
module-info.javafile to explicitlyrequiresthose modules.
This approach is more complex and not recommended for most applications.
Workaround
If modularization is too difficult:
-
Skip
jlinkand instead usemaven-shade-pluginorjpackageto bundle dependencies into a runnable JAR or native installer. -
For development and testing, you can still run the app via:
mvn javafx:run
Comments
0 comments
Please sign in to leave a comment.