如何将Spring Boot可执行Jar转为Exe?Launch Maven Plugin生成的Exe双击无控制台
将Spring Boot可执行Jar转换为Exe文件的解决方案
一、解决Launch Maven Plugin生成Exe无控制台的问题
你遇到的问题大概率是插件默认将应用打包为窗口应用模式,而非控制台模式。只需在插件配置中显式开启控制台输出即可:
在你的pom.xml中,找到Launch Maven Plugin的配置块,添加<console>true</console>参数:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-launch4j-plugin</artifactId> <version>2.2.0</version> <!-- 使用最新稳定版本 --> <executions> <execution> <id>launch4j</id> <phase>package</phase> <goals> <goal>launch4j</goal> </goals> <configuration> <headerType>console</headerType> <!-- 指定为控制台应用 --> <outfile>target/your-app.exe</outfile> <jar>target/your-app.jar</jar> <errTitle>Error</errTitle> <classPath> <mainClass>com.example.YourApplication</mainClass> </classPath> <jre> <minVersion>11</minVersion> <!-- 指定你的Spring Boot依赖的JRE版本 --> </jre> <console>true</console> <!-- 强制开启控制台 --> </configuration> </execution> </executions> </plugin>
重新执行mvn package生成Exe,双击后应该就能看到控制台窗口了。
二、其他常用的Jar转Exe工具
1. 使用Launch4j独立工具
如果不想用Maven插件,可以直接使用Launch4j图形化工具:
- 打开工具后,选择「Console」作为头部类型
- 在「Jar」标签页指定Spring Boot可执行Jar路径,填写主类全名
- 在「JRE」标签页设置最小JRE版本
- 点击「Build Wrapper」生成Exe文件
2. 使用JDK自带的jpackage工具(JDK14+)
JDK14及以上版本自带的jpackage工具可直接生成原生Exe,无需额外依赖:
在命令行进入Jar所在目录,执行以下命令:
jpackage --type exe ^ --input . ^ --main-jar your-app.jar ^ --main-class com.example.YourApplication ^ --name YourApp ^ --dest target ^ --console
参数说明:
--console:指定为控制台应用,双击会弹出命令行窗口--input:Jar文件所在目录--main-jar:你的Spring Boot可执行Jar文件名--main-class:应用的主类全名
若需要单文件分发,还可以添加--runtime-image参数,将JRE一起打包进Exe。
内容的提问来源于stack exchange,提问作者Sandy
相关产品推荐
相关产品推荐

