Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch4J Plugin creates an EXE (along with JAR) but EXE cannot find main class in Spring boot

I wrote a plugin to create an EXE and JAR for my project with the help of Launch4J plugin. However, on executing the EXE file, I get the error -

Error: Could not find or load main class

However, I run the JAR by giving java -jar app.jar , it runs perfectly.

This is my plugin section

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

The EXE is present in my target folder. But I cannot execute it as it says Main Class not found (despite adding it in the plugin details)

like image 568
Prateek Narendra Avatar asked Oct 20 '25 15:10

Prateek Narendra


1 Answers

Try to tun the maven build using the following command for any maven project.

mvn clean package or mvn clean install

With regard to Launch4j, you have to run the following command.

mvn clean package

For more details on Launch4j, you can check below the link. https://github.com/lukaszlenart/launch4j-maven-plugin/blob/master/src/main/resources/README.adoc

But in this project, I see you are using spring boot. If my understanding is correct, you want to create an exe file out of spring boot. If this is the case.

Can you try to use the below changes for launch4j and check.

<classPath>                    
   <mainClass>org.springframework.boot.loader.JarLauncher</mainClass>                                
   <addDependencies>true</addDependencies>
   <preCp>anything</preCp>
</classPath>
like image 188
Sambit Avatar answered Oct 23 '25 05:10

Sambit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!