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

使用Maven+Launch4j打包Java17程序为EXE时遇指定错误

问题原因与解决方案

核心原因

Java 17 对反射访问私有成员的限制更严格,你使用的 maven-shade-plugin 或 launch4j-maven-plugin 旧版本中,存在通过反射访问 java.util.Properties 私有字段 defaults 的代码,触发了模块系统的访问限制,导致报错。

具体解决步骤

1. 升级Maven插件版本

  • 更新 maven-shade-plugin 到3.3.0+版本(该版本已修复Java 9+的反射兼容问题),pom.xml配置示例:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.5.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>你的主类全路径</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
  • 更新 launch4j-maven-plugin 到2.5.0+版本(适配Java 11+),pom.xml配置示例:
    <plugin>
        <groupId>com.akathist.maven.plugins.launch4j</groupId>
        <artifactId>launch4j-maven-plugin</artifactId>
        <version>2.5.2</version>
        <executions>
            <execution>
                <id>l4j-clui</id>
                <phase>package</phase>
                <goals>
                    <goal>launch4j</goal>
                </goals>
                <configuration>
                    <headerType>console</headerType>
                    <outfile>target/你的程序名.exe</outfile>
                    <jar>target/你的jar包名.jar</jar>
                    <jre>
                        <minVersion>17</minVersion>
                    </jre>
                    <!-- 图标路径问题可暂时注释,先确保打包成功 -->
                    <!--<icon>src/main/resources/icon.ico</icon>-->
                </configuration>
            </execution>
        </executions>
    </plugin>
    

2. 添加模块访问权限参数

如果升级插件后仍报错,需在配置中添加JVM参数,允许反射访问目标字段:

  • 在shade插件的configuration中添加:
    <jvmArguments>
        --add-opens java.base/java.util=ALL-UNNAMED
    </jvmArguments>
    
  • 或在launch4j的jre配置中添加:
    <jre>
        <minVersion>17</minVersion>
        <jvmOptions>
            <jvmOption>--add-opens java.base/java.util=ALL-UNNAMED</jvmOption>
        </jvmOptions>
    </jre>
    

3. 排查冲突依赖

检查项目依赖中是否有旧版本的Apache Commons Configuration等直接操作Properties.defaults的库,替换为Java 17兼容版本(比如commons-configuration升级到2.8.0+)。

4. 放弃WAR打包

你的程序是控制台CMD程序,WAR包适用于Web应用,完全不匹配,专注JAR+EXE的打包流程即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 00:30:04