使用Gradle构建Apache POI 5.2.4时Java工具链匹配失败求助
Apache POI 5.2.4 Gradle构建失败问题排查与解决
问题背景
此前使用Apache工具包只需导入附带jar即可使用,但Apache POI 5.2.4需通过Gradle构建。在Eclipse 2022-03中通过「导入→现有Gradle项目」构建,以及命令行执行构建均失败,报错如下:
提供的阶段操作执行失败,配置项目':poi'时出现问题。评估项目':poi'时出错,无法计算任务':poi:compileTestJava'的'javaCompiler'属性值。未找到符合请求规格{languageVersion=8, vendor=any, implementation=vendor-specific}的工具链(适用于WINDOWS x86_64系统)。本地未安装匹配的工具链,且未配置工具链下载仓库。
未修改Apache提供的build.gradle,其工具链配置代码如下:
java { toolchain { languageVersion = JavaLanguageVersion.of(jdkVersion) if (jdkVendor != '') vendor = JvmVendorSpec.matching(jdkVendor) } withJavadocJar() withSourcesJar() }
命令行执行gradle.bat的完整输出:
poi-src-5.2.4-20230921-5.2.4>gradle.bat Configuration on demand is an incubating feature. > Configure project : Trying to override old definition of datatype junit FAILURE: Build failed with an exception. * Where: Build file 'C:\Users\x\eclipse-workspace\poi-src-5.2.4-20230921-5.2.4\poi\build.gradle' line: 82 * What went wrong: A problem occurred evaluating project ':poi'. > Failed to calculate the value of task ':poi:compileTestJava' property 'javaCompiler'. > No matching toolchains found for requested specification: {languageVersion=8, vendor=any, implementation=vendor-specific} for WINDOWS on x86_64. > No locally installed toolchains match and toolchain download repositories have not been configured. * Try: > Learn more about toolchain auto-detection at https://docs.gradle.org/8.4/userguide/toolchains.html#sec:auto_detection. > Learn more about toolchain repositories at https://docs.gradle.org/8.4/userguide/toolchains.html#sub:download_repositories. > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. For more on this, please refer to https://docs.gradle.org/8.4/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation. BUILD FAILED in 1s
问题原因
- POI 5.2.4的构建脚本默认指定Java 8作为编译测试代码的工具链版本,而你本地仅安装了Java 21,且未配置Gradle自动下载对应JDK的仓库。
- 尝试安装旧版Java时,可能因环境变量配置错误、JDK安装路径包含特殊字符等,导致Gradle无法识别本地工具链。
解决方法
方法一:安装Java 8并配置Gradle识别
- 下载并安装Java 8 JDK(需为JDK而非JRE),安装路径避免中文或特殊字符。
- 让Gradle检测到Java 8:
- 设置环境变量
JAVA_HOME_8指向Java 8的安装目录; - 或临时将Java 8的
bin目录加入系统PATH(仅临时解决,不推荐长期使用)。
- 设置环境变量
- 重新执行
gradle.bat即可。
方法二:修改构建脚本适配现有Java版本
若不想安装Java 8,可修改POI的构建配置:
- 在POI源码根目录找到
gradle.properties文件,将jdkVersion的值从8改为21。 - 若修改后出现测试代码兼容性问题,可跳过测试任务构建核心jar:
gradle.bat build -x test
内容的提问来源于stack exchange,提问作者EngineerWithJava54321
相关产品推荐
相关产品推荐

