1

On windows, how do I create a standalone .exe executable from a spring-boot project ?

I would like a binary executable that is platform-dependent, and does not rely on an installed JRE. This exec should contain the JRE, all libs/deps (über-jar inside...) and should be able to run on a fresh install of Windows without Java.

I use spring-boot-maven-plugin in my pom, but mvn package stops at generating an "executable jar", which still relies on a JRE being installed on the system.

Ideally, the build process would be managed by maven lifecycle, so I tried to use the launch4j-maven-plugin, but the EXE generated keeps redirecting me to some oracle.com web page. The app runs fine within IntelliJ "run", JRE 1.8 is in the usual C:Program Files\java, %JAVA_HOME% points to JDK 1.16, in C:Program Files\AdoptOpenJDK

Here is the source of my app, single class, console app :

@SpringBootApplication
public class HeyApplication implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication.run(HeyApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) {
        System.out.println("Hey there !"); // STDOUT
    }
}

and an extract from the pom.xml:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <version>2.1.1</version>
                <executions>
                    <execution>
                        <id>l4j-clui</id>
                        <phase>package</phase>
                        <goals><goal>launch4j</goal></goals>
                        <configuration>
                            <headerType>console</headerType>
                            <outfile>hey.exe</outfile>
                            <jar>target/hey-0.0.1-SNAPSHOT.jar</jar>
                            <errTitle>errr</errTitle>
                            <classPath>
                                <mainClass>test.HeyApplication</mainClass>
                                <addDependencies>true</addDependencies>
                                <preCp>anything</preCp>
                            </classPath>
                            <jre>
                                <minVersion>1.8</minVersion>
                            </jre>
                            <versionInfo>
                                <fileVersion>1.2.3.4</fileVersion>
                                <txtFileVersion>txt file version?</txtFileVersion>
                                <fileDescription>a description</fileDescription>
                                <copyright>my copyright</copyright>
                                <productVersion>4.3.2.1</productVersion>
                                <txtProductVersion>txt product version</txtProductVersion>
                                <productName>E-N-C-C</productName>
                                <internalName>ccne</internalName>
                                <originalFilename>original.exe</originalFilename>
                            </versionInfo>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Also, is there, by any chance, a Spring way of doing this JRE wrapping ?

jmbourdaret
  • 211
  • 1
  • 6
  • I don't know of any Spring way, but maybe this helps: https://stackoverflow.com/questions/7071133/how-to-bundle-a-jre-with-launch4j – jhyot May 08 '21 at 18:01
  • It helps, thanks. ```launch4j``` is probably the way to go. I am also looking at https://github.com/fvarrui/JavaPackager/ which is sort of all-in-one maven plugin . it is also based on launch4j, but I'm struggling to configure it correctly. – jmbourdaret May 12 '21 at 21:51

0 Answers0