Launch4j Maven 插件版本 2.0.1 找不到或加载 main class

Launch4j Maven plugin version 2.0.1 Could not find or load main class

首先,我在互联网上和 Whosebug 上阅读了很多关于这个问题的帖子,当没有解决问题时,我决定问问自己。

我正在尝试使用 Launch4j maven 插件为我的 spring 启动应用程序创建一个简单的 exe。生成的exe找不到主class。我的命令行出现以下错误:

Error: Could not find or load main class com.XXX.scheduler.SchedulerApplication
Caused by: java.lang.ClassNotFoundException: com.XXX.scheduler.SchedulerApplication

下面是我的 pom.xml,我在其中尝试了多种组合来让这个东西工作。 注意:仅罐子就可以正常工作。

<?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>

好的,在对 Whosebug 本身进行更多搜索后,我从下面找到了答案 post。

问题是生成的“test.exe”找不到主class,答案是它不应该指向主class,而是应该寻找jar launcher class

我已经按照 post 中的建议更新了 maven 插件 <classPath> 标签,它非常有效。希望对您有所帮助。

<classPath>
    <mainClass>org.springframework.boot.loader.JarLauncher</mainClass>
    <addDependencies>true</addDependencies>
    <preCp>anything</preCp>
</classPath>