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

JavaFX项目使用jpackage-maven-plugin打包依赖与JVM问题求助

解决JavaFX项目jpackage打包的依赖与JVM问题

一、修复JVM打包失败问题

不要直接指定<runtimeImage>${java.home}</runtimeImage>,而是让jlink自动构建自定义运行时,同时确保:

  • 你使用的JDK已正确放入JavaFX jmods文件(确认${java.home}/jmods下存在javafx相关jmod文件)。
  • 不再手动指定runtimeImage参数,交给jlink根据模块配置生成运行时。

二、处理自动模块无法被jlink识别的问题

jlink仅支持带module-info.java的显式模块,而commons-math3属于自动模块(无module-info),有两种解决方式:

方式1:将自动模块转为显式模块

给commons-math3生成并添加module-info:

  1. 创建临时目录,比如temp/commons.math3
  2. 生成模块描述文件:
    jdeps --generate-module-info temp/commons.math3 target/lib/commons-math3-3.6.1.jar
    
  3. 编译生成的module-info.java:
    javac -d temp/commons.math3 temp/commons.math3/module-info.java
    
  4. 将编译后的module-info.class合并到原JAR:
    jar uf target/lib/commons-math3-3.6.1.jar -C temp/commons.math3 module-info.class
    

方式2:将自动模块作为非模块依赖打包

不把自动模块加入jlink的模块路径,而是作为普通classpath依赖打包到安装包的app/lib目录:

  • 保持maven-dependency-plugin将所有依赖复制到target/lib
  • 在jpackage配置中拆分模块路径与classpath依赖:
    • 显式模块(如javafx-base、sqlite-jdbc)放入<modulePath>
    • 自动模块(commons-math3、commons-codecs等)通过<arguments>指定classpath,同时用<appResources>确保文件被复制

三、完整jpackage-maven-plugin配置示例

<plugin>
    <groupId>org.openjfx</groupId>
    <artifactId>jpackage-maven-plugin</artifactId>
    <version>0.0.8</version>
    <executions>
        <execution>
            <id>default-cli</id>
            <phase>package</phase>
            <goals>
                <goal>jpackage</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>你的主类全路径(如com.example.MainApp)</mainClass>
        <name>Example</name>
        <dest>${project.build.directory}/jpackage</dest>
        <!-- 配置显式模块路径 -->
        <modulePath>
            <path>${project.build.directory}/lib</path>
        </modulePath>
        <!-- 指定需要打包的显式模块 -->
        <addModules>
            <module>你的应用模块名</module>
            <module>javafx.controls</module>
            <module>javafx.fxml</module>
            <module>org.sqlite.jdbc</module>
        </addModules>
        <!-- 配置自动模块的classpath(Windows用;分隔,Linux/Mac用:) -->
        <arguments>
            <argument>--class-path</argument>
            <argument>${project.build.directory}/lib/commons-math3-3.6.1.jar;${project.build.directory}/lib/commons-codec-1.15.jar;...</argument>
        </arguments>
        <!-- 确保所有依赖被复制到安装包的app/lib目录 -->
        <appResources>
            <resource>${project.build.directory}/lib</resource>
        </appResources>
    </configuration>
</plugin>

四、额外注意事项

  • 确保项目的module-info.java正确声明依赖:
    module 你的应用模块名 {
        requires javafx.controls;
        requires javafx.fxml;
        requires org.sqlite.jdbc;
        // 自动模块直接用requires声明
        requires commons.math3;
        requires commons.codec;
        opens 你的FXML所在包 to javafx.fxml;
        exports 你的对外包;
    }
    
  • 打包前先执行mvn clean package,确保target/lib下的依赖是最新的。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 16:05:52