问题描述
首先,我在互联网和 stackoverflow 上阅读了很多关于这个问题的帖子,但没有解决问题,我决定问问自己。
我正在尝试使用 launch4j maven 插件为我的 Spring Boot 应用程序创建一个简单的 exe。生成的exe找不到主类。我的命令行出现以下错误:
Error: Could not find or load main class com.XXX.scheduler.SchedulerApplication
Caused by: java.lang.classNotFoundException: com.XXX.scheduler.SchedulerApplication
下面是我的 pom.xml,我在其中尝试了多种组合来让这个东西正常工作。 注意:单独使用 jar 就可以了。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>com.XXX</groupId>
<artifactId>scheduler</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>scheduler</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<!-- <properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties> -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<version>2.0.1</version>
<executions>
<execution>
<id>l4j-clui</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<dontWrapJar>true</dontWrapJar>
<headerType>console</headerType>
<!-- <headerType>gui</headerType> -->
<!-- <jar>${project.build.directory}/scheduler-0.0.1-SNAPSHOT.jar</jar> -->
<jar>${artifactId}-${version}.jar</jar>
<!-- <jar>${project.build.directory}/${artifactId}-${version}.jar</jar> -->
<outfile>${project.build.directory}/test.exe</outfile>
<downloadUrl>http://java.com/download</downloadUrl>
<classpath>
<mainClass>com.XXX.scheduler.SchedulerApplication</mainClass>
<!-- <mainClass>SchedulerApplication</mainClass> -->
<!-- <mainClass>com/XXX/scheduler/SchedulerApplication</mainClass> -->
<addDependencies>false</addDependencies>
<preCp>anything</preCp>
</classpath>
<!-- <icon>src/main/resources/icon/application.ico</icon> -->
<jre>
<!-- <path>../jdk15</path> -->
<!-- <jdkPreference>preferJdk</jdkPreference> -->
<jdkPreference>preferJre</jdkPreference>
<minVersion>11</minVersion>
<!-- <minVersion>1.8.0_212</minVersion> -->
<initialHeapSize>256</initialHeapSize>
<maxHeapSize>3000</maxHeapSize>
<!-- <minVersion>1.7.0</minVersion> -->
<!-- <minVersion>1.6.0</minVersion> -->
<!-- <jdkPreference>preferJre</jdkPreference> -->
</jre>
<versionInfo>
<fiLeversion>1.0.0.0</fiLeversion>
<txtFiLeversion>${project.version}</txtFiLeversion>
<fileDescription>${project.name}</fileDescription>
<copyright>2021 XXX.com</copyright>
<productVersion>1.0.0.0</productVersion>
<txtProductVersion>1.0.0.0</txtProductVersion>
<productName>${project.name}</productName>
<companyName>XXX.com</companyName>
<internalName>rfrcscheduler</internalName>
<originalFilename>test.exe</originalFilename>
</versionInfo>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
解决方法
好的,在对 stackoverflow 本身进行了更多搜索之后,我从下面的帖子中找到了答案。 Launch4J Plugin creates an EXE (along with JAR) but EXE cannot find main class in Spring boot
问题是生成的“test.exe
”找不到主类,答案是它不应该指向主类,而是应该查找 jar launcher class
我已按照帖子中的建议更新了 Maven 插件 <classPath>
标签,它的作用非常出色。我希望这会有所帮助。
<classPath>
<mainClass>org.springframework.boot.loader.JarLauncher</mainClass>
<addDependencies>true</addDependencies>
<preCp>anything</preCp>
</classPath>