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

JavaFX+Java21+Gradle项目集成Apache POI依赖失败求助

Apache POI集成jpackage问题排查与解决方案

一、模块化应用方案(解决createMergedModule找不到POI模块问题)

问题根源

Apache POI的jar包属于自动模块(无内置module-info.java),直接在自定义模块中声明requires时,Gradle可能无法自动识别其模块名,导致createMergedModule任务报错。

配置调整

  1. 修正module-info.java
    明确引入POI的自动模块名(POI的自动模块名固定为org.apache.poi.poi和org.apache.poi.ooxml):

    module your.app.module {
        requires javafx.controls;
        requires javafx.fxml;
        // 引入POI自动模块
        requires org.apache.poi.poi;
        requires org.apache.poi.ooxml;
        
        // 根据项目需求配置包开放/导出
        opens com.your.app.ui to javafx.fxml;
        exports com.your.app;
    }
    
  2. 调整build.gradle配置
    启用模块路径自动推断,并确保jlink/createMergedModule任务包含POI模块:

    plugins {
        id 'java'
        id 'application'
        id 'org.openjfx.javafxplugin' version '0.1.0'
        id 'org.beryx.jlink' version '2.25.0'
    }
    
    repositories {
        mavenCentral()
    }
    
    // 启用模块路径推断
    java {
        modularity.inferModulePath = true
    }
    
    javafx {
        version = '21'
        modules = ['javafx.controls', 'javafx.fxml']
    }
    
    dependencies {
        implementation 'org.apache.poi:poi:5.2.5'
        implementation 'org.apache.poi:poi-ooxml:5.2.5'
    }
    
    application {
        mainModule = 'your.app.module'
        mainClass = 'com.your.app.Main'
    }
    
    jlink {
        mergedModule {
            // 显式声明需要合并的POI模块
            requires 'org.apache.poi.poi';
            requires 'org.apache.poi.ooxml';
        }
        options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    }
    

二、非模块化应用方案(解决GroovyRuntimeException类型转换问题)

问题根源

org.beryx.runtime插件配置语法错误(如参数类型不匹配、多余配置项),导致出现ArrayList转Void的异常。

配置调整

移除多余的模块化配置,确保插件参数符合要求:

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.1.0'
    id 'org.beryx.runtime' version '1.15.0'
}

repositories {
    mavenCentral()
}

javafx {
    version = '21'
    modules = ['javafx.controls', 'javafx.fxml']
}

dependencies {
    implementation 'org.apache.poi:poi:5.2.5'
    implementation 'org.apache.poi:poi-ooxml:5.2.5'
}

application {
    // 非模块化直接指定主类,无需mainModule
    mainClass = 'com.your.app.Main'
}

runtime {
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    jpackage {
        imageName = 'YourApp'
        installerName = 'YourAppInstaller'
        // 根据目标系统选择安装包类型(exe/dmg/deb等)
        installerType = 'exe'
    }
}

注意:非模块化模式下需移除项目中的module-info.java文件,避免冲突。

内容的提问来源于stack exchange,提问作者SM-Net

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 14:14:55