Launch4J插件创建一个EXE(以及JAR),但EXE在Spring Boot中找不到主类

时间:2019-05-23 09:18:56

标签: java maven spring-boot maven-plugin launch4j

我编写了一个插件,借助Launch4J插件为我的项目创建EXE和JAR。但是,在执行EXE文件时,出现错误-

Error: Could not find or load main class

但是,我通过给java -jar app.jar来运行JAR,它运行得很好。

这是我的插件部分

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.mycompany.tool.orchestrator.command.CommandHandler</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <version>1.7.16</version>
                <executions>
                    <execution>
                        <id>l4j-clui</id>
                        <phase>package</phase>
                        <goals>
                            <goal>launch4j</goal>
                        </goals>
                        <configuration>
                            <headerType>console</headerType>
                            <outfile>target/apidast.exe</outfile>
                            <jar>${project.build.directory}/${artifactId}-${version}.jar</jar>
                            <errTitle>encc</errTitle>
                            <classPath>
                                <mainClass>com.mycompany.tool.orchestrator.command.CommandHandler</mainClass>
                                <!--Not sure what the next 2 lines are for -->
                                <addDependencies>true</addDependencies>
                                <preCp>anything</preCp>
                            </classPath>
                            <jre>
                                <minVersion>1.8.0_212</minVersion>
                                <!--Not sure what these opts are for -->
                                <opts>
                                    <opt>-Djava.endorsed.dirs=./endorsed</opt>
                                </opts>
                            </jre>
                            <versionInfo>
                                <fileVersion>0.0.1.0</fileVersion>
                                <txtFileVersion>0.0.1.0</txtFileVersion>
                                <fileDescription>${project.name}</fileDescription>
                                <copyright>My Company.</copyright>
                                <productVersion>0.0.1.0</productVersion>
                                <txtProductVersion>${version}</txtProductVersion>
                                <productName>${project.name}</productName>
                                <originalFilename>apidast.exe</originalFilename>
                                <!-- internalName is mandatory -->
                                <internalName>apidast</internalName>
                            </versionInfo>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

该EXE位于我的目标文件夹中。但是我无法执行它,因为它说找不到主类(尽管在插件详细信息中添加了它)

1 个答案:

答案 0 :(得分:0)

尝试对所有Maven项目使用以下命令来调整Maven构建。

  

mvn全新安装包或mvn全新安装

对于Launch4j,您必须运行以下命令。

  

MVN清洁包

有关Launch4j的更多详细信息,您可以检查下面的链接。 https://github.com/lukaszlenart/launch4j-maven-plugin/blob/master/src/main/resources/README.adoc

但是在这个项目中,我看到您正在使用spring boot。如果我的理解是正确的,则您想在Spring Boot之外创建一个exe文件。如果是这样的话。

您能否尝试对启动4j使用以下更改并进行检查?

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