launch4j-maven-plugin compiles exe program

Directory

1. Introduce the launch4j-maven-plugin plug-in in the pom.xml file

2. Compile and start

1. Execute the following command on the command line to package the program:

2. Execute the following command on the command line to convert the jar package to exe:

2. Label analysis

1. headerType tag

2. Outfile label

3. jar label

4. errorTitle tag

5. classPath tag

6. versionInfo tag

7. icon label

8. jre tag

3. Summary


1. Introduce the launch4j-maven-plugin plug-in in the pom.xml file

<build>
        <plugins>
            <plugin>
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <version>1.7.25</version>
                <executions>
                    <execution>
                        <id>l4j-clui</id>
                        <phase>package</phase>
                        <goals><goal>launch4j</goal></goals>
                        <configuration>
                            <!--Run mode, console -->
                            <headerType>gui</headerType>

                            <!--Output exe file-->
                            <outfile>${project.build.directory}/WeiKeDownload.exe</outfile>

                            <!--Output jar-->
                            <jar>${project.build.directory}/${artifactId}-${version}.jar</jar>

                            <!--Error Title-->
                            <errTitle>WeiKeDownloadError</errTitle>
                            <classPath>
                                <!--Replace with your own main class-->
                                <mainClass>com.download.weikeJavaFX.WeiKeApplication</mainClass>
                                <addDependencies>true</addDependencies>
                                <preCp>anything</preCp>
                            </classPath>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

The version used here is 1.7.25, and the latest known version is 1.7.30. Since I failed to load it, I did not quote it.

Second, compile and start

1. Execute the following command on the command line to package the program:

mvn clean package

Note that the jar package is packaged at this time.

2. Execute the following command on the command line to convert the jar package to exe:

mvn launch4j:launch4j

After execution, an .exe executable file will be generated in the target directory.

The above are the steps to package a Java program into an exe using the launch4j-maven-plugin plug-in.

Second, label analysis

1, headerType tag

The headerType tag in launch4j-maven-plugin is used to set the window type of the generated executable file. The specific optional categories are as follows:

  • console: console window, without UI.
  • gui: A window with an interface (with attributes such as icon), and the icon and information of the form can be seen in the Windows operating system.
  • service: used to create Windows Service (Windows Service), usually used in background programs.

Among them, if it is set to console type, the plain text command line interface will be used; if it is set to gui type, the icon and information of the window can be customized; if it is set to service type, it will run as a background service, even if the console window Being closed will not affect the operation of the service.

2, outfile tag

The outfile tag in the launch4j-maven-plugin plugin is used to set the output path and name of the generated executable file. The content set by this tag is the full path of the generated executable file. If only the file name is specified, the executable file will be generated in the working directory of the plugin.

The attributes of this tag are as follows:

  • value: Set the full path and name of the generated executable file.
  • replaceExtension: Specifies a Boolean value, if true, the extension of the file specified by outfile will be replaced with exe. Example: Replace “myapp.jar” with “myapp.exe”.
  • createHeader: Specify a Boolean value. If it is true, a file header information will be created at the top of the executable file, and related information can be viewed in the file properties.

This flag must be set, otherwise the plugin will not be able to generate executable files.

3, jar tag

The jar tag in the launch4j-maven-plugin plugin is used to set the path of the jar package that the executable file to be generated depends on. The attributes of this tag are the following two:

  • value: Set the relative path or absolute path of the jar package that the executable file to be generated depends on.
  • path: Set the class path of the jar package that the executable file to be generated depends on. This path is defined relative to the path of the generated executable file.

When using launch4j-maven-plugin to generate executable files, you need to specify which jar packages to include in the pom.xml file. If the executable file to be generated depends on other jar packages, you need to add the corresponding dependencies in the configuration file of the plug-in. The specific method is as follows:

<build>
        <plugins>
            <plugin>
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <version>1.7.25</version>
                <executions>
                    <execution>
                        <id>l4j-clui</id>
                        <phase>package</phase>
                        <goals><goal>launch4j</goal></goals>
                        <configuration>
                            <!--Run mode, console -->
                            <headerType>gui</headerType>

                            <!--Output exe file-->
                            <outfile>${project.build.directory}/WeiKeDownload.exe</outfile>

                            <!--Output jar-->
                            <jar>${project.build.directory}/${artifactId}-${version}.jar</jar>
                            <!--reference other jar-->
                            <jar>
                              <path>lib/dependency1.jar</path>
                              <path>lib/dependency2.jar</path>
                            </jar>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

4, errorTitle label

The errTitle tag in the launch4j-maven-plugin plugin is used to set the title of the window that pops up when an error occurs in the generated executable file. The attribute of this tag has one of the following:

  • value: Set the title of the window that the generated executable will pop up when an error occurs.

When some unexpected errors occur in the generated executable file, such as the dependent library cannot be found, the file format is incorrect, etc., the generated program will report an error and pop up a window to display the error message. At this time, the value of the label will be used as the popup The title of the window. Therefore, setting an appropriate value for the errTitle tag can make it easier for users to understand the error message and quickly locate the problem.

5, classPath tag

The classPath tag in the launch4j-maven-plugin plugin is used to set the classpath of the generated executable. There are three sub-tabs of this tab:

  • mainClass: Set the main class of the program, that is, the entry of the executable file.
  • preCp: Set the prefix path of classpath.
  • postCp: Set the suffix path of classpath.

The mainClass tag in the classPath tag must be set, because it specifies the main class of the program. If the tag is set to an empty string or not set, the generated executable file will not run normally.

When configuring the classPath tag, the path in this tag should correspond to the path of the generated executable file, and you can use a relative path or an absolute path. Depending on different operating systems, the format of the path may be different, so it needs to be adjusted according to the actual situation.

For example, in the following configuration, we specify the classpath of mainClass and specify a folder named “lib” as the prefix path for preCp:


com.example.Main

lib

In this way, the generated executable file can search and load its dependent library files under the preset prefix path. If there are multiple dependent libraries, they can be separated by commas in the preCp and postCp tags.

6, versionInfo tag

The versionInfo tag in the launch4j-maven-plugin plugin is used to set the version information of the generated executable file. The attributes of this tag are as follows:

  • fileVersion: file version number.
  • txtFileVersion: Text file version number.
  • fileDescription: file description.
  • productVersion: Product version number.
  • txtProductVersion: Text product version number.
  • productName: product name.
  • companyName: Company name.
  • internalName: internal name.
  • originalFilename: original file name.

The version information allows the user to know the version of the program, the publishing company and other information when viewing the properties of the file, which is one of the most important elements when the program is released.

For example, in the following configuration, we set the file version number as “1.0.0.0”, the product version number as “2.0”, the product name as “myapp”, and the company name as “example inc. ” with the internal name “myapp”:


<configuration>
    <versionInfo>
      <fileVersion>1.0.0.0</fileVersion>
      <productVersion>2.0</productVersion>
      <productName>myapp</productName>
      <companyName>example inc.</companyName>
      <internalName>myapp</internalName>
    </versionInfo>
</configutation>

Among them, fileVersion and productVersion must be set, while other tags can be set according to the actual situation. If some tags are not set, the properties will use default values.

7, icon tag

The icon tag in the launch4j-maven-plugin plugin is used to set the icon of the generated executable file. The attribute of this tag has one of the following:

  • value: Set the path to the icon for the generated executable.

To set an icon, we need to specify the value attribute as the path of the file containing the icon. This file can be an image file in ICO format, BMP format or PNG format. Prefix the path with “file:” to specify a resource on the network, or specify an absolute path. If there is no value attribute, the operating system’s default icon is used by default.

For example, in the following configuration, we specify an icon file located at “/path/to/myicon.ico”:

file:/path/to/myicon.ico

In this way, the generated executable file will automatically add the icon we specified.

8, jre tag

The jre tag in the launch4j-maven-plugin plugin is used to set the JRE version and path required by the generated executable file. The attributes of this tag are as follows:

  • minVersion: Sets the minimum version of JRE required by the generated executable. If the JRE version on the system is lower than this version, the program will not run. This property is required.
  • maxVersion: Sets the highest version of the JRE required by the generated executable. If the JRE version on the system is higher than this version, the program will not run.
  • jdkPreference: Sets the JDK preference used by the generated executable.
  • initialHeapSize: Used to set the initial heap memory size of the generated executable file.
  • path: It is used to specify the path of JRE. If path is not set, the plug-in will use the JRE path installed in the current system by default (by default, the plug-in uses the JRE specified by the JAVA_HOME environment variable).

For example, in the following configuration, we specify that the generated executable file requires JRE 1.8 and above, and set the JRE path to the relative path “../java/jre”:

<!--Configure jre-->
<jre>
    <bundledJre64Bit>false</bundledJre64Bit>
    <bundledJreAsFallback>false</bundledJreAsFallback>
    <jdkPreference>preferJdk</jdkPreference>
    <initialHeapSize>128</initialHeapSize>
    <maxHeapSize>1024</maxHeapSize>

    <!--jre path, put the generated exe file in the same directory as jre, and name the jre folder jre-->
    <path>../java/jre</path>
</jre>

In this way, the generated executable will automatically depend on the JRE version and path we specified.

9. manifest label

The manifest tag in the launch4j-maven-plugin plugin is used to set the information in the Manifest file of the generated executable file. The Manifest file contains the meta information of the program and the relevant parameters for starting the program. The attribute of this tag has one of the following:

  • file: used to specify the path of the Manifest file.

When configuring the Manifest file, we need to set the path of the file in the file attribute. If the file attribute is not set, the plugin will not use any Manifest file, and the program will use the default Manifest information. The format of the Manifest file is usually in text format, and all parameters as follows are defined in the file:

  • Main-Class: Specifies the main class file name, the class containing the main method.
  • Class-Path: Specify the path of all class libraries required by the program, and multiple class library paths are separated by spaces.
  • Boot-Class-Path: Specifies the path of additional class files that need to be loaded when the program starts.
  • JavaFX-Version: Specifies the JavaFX version that can be used.
  • Mainfest-Version: Specifies the version of the Manifest file.
  • Permissions: Specify security permissions that require special handling.
  • Codebase: Specifies the domain where the program resides.
  • Application-Name: Specify the name of the application.
  • Implementation-Vendor: Specifies the name of the program implementation vendor.

For example, in the following configuration we would use the Manifest file located at “/path/to/manifest.mf”:

/path/to/manifest.mf

In this way, the generated executable will automatically use the option information in the Manifest file we specified.

10. Set the exe file administrator permission to start

Following the use of the above 9 tags, quote the content of the manifest file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
            <requestedPrivileges>
                <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
            </requestedPrivileges>
        </security>
    </trustInfo>
</assembly>

3. Summary

At this point, the initial use of the launch4j-maven-plugin plug-in is completed, and the corresponding files can be generated by packaging the mvn command. It should be noted that if the location of the lib package compiled by referencing jre or other maven plug-ins is set, the relative path must be guaranteed The corresponding location of the exe file has a target reference, and the absolute path must ensure that the file actually exists, otherwise the startup will be abnormal.