更新:删除了之前的回答,并用经过测试的工作示例进行替换。
更新2:此pom.xml现在下载JRE tgz并解压缩它,launch4j exe将使用它并正常工作。我添加了注释来解释它的工作原理。
我建议仅使用32位exe和JRE。使用64位JRE的唯一原因是如果您的程序需要使用超过4GB的RAM。
当然,现在您需要一个安装程序,将所有内容安装到Program Files中。我过去使用过NSIS进行此操作。学习曲线对于NSIS来说不太陡峭。
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.akathist.encc</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.oracle.java</groupId>
<artifactId>jre</artifactId>
<classifier>win32</classifier>
<type>tgz</type>
<version>1.8.0_131</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>alfresco</id>
<url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>com.oracle.java</includeGroupIds>
<includeTypes>tgz</includeTypes>
<includeArtifactIds>jre</includeArtifactIds>
<includeClassifiers>win32</includeClassifiers>
<outputDirectory>target/win32</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<executions>
<execution>
<id>l4j-clui</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>console</headerType>
<outfile>target/encc.exe</outfile>
<jar>target/mavenproject1-1.0-SNAPSHOT.jar</jar>
<errTitle>encc</errTitle>
<classPath>
<mainClass>com.akathist.encc.Clui</mainClass>
<addDependencies>false</addDependencies>
<preCp>anything</preCp>
</classPath>
<jre>
<path>./win32/java</path>
</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>
</project>
更新3:令人遗憾的是,没有任何官方或者最新的Maven仓库提供您所需的JRE版本。您可以自己搭建一个Maven仓库,并将所需的JRE版本添加到其中。但需要注意,您必须在每次新版本发布后及时更新仓库,并在发布新版本前进行测试。Java的新版本通常在每个月的第三个星期二发布,您可以设置一个提醒来检查是否发布了新版本,并进行下载。由于许可协议的检查,自动化此过程可能会有些麻烦。这篇文章可能会有所帮助,但您可能无法通过这种方式下载tar.gz版本的JRE:Java check latest version programmatically
如果您希望支持多个平台,则搭建自己的Maven仓库是一个不错的选择。您可以搭建自己的仓库,并使用此方法在每次发布新版本时添加新的JRE tar.gz文件:https://dev59.com/YWYr5IYBdhLWcg3wQH--#29261502
最简单的选择是使用您正在构建的JRE版本。只要使用32位JRE进行构建,即可支持Windows 32位和64位。您可以在有时间测试新版本时偶尔进行更新。以下是一个可用的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.akathist.encc</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/win32/java</outputDirectory>
<resources>
<resource>
<directory>${java.home}</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<executions>
<execution>
<id>l4j-clui</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>console</headerType>
<outfile>target/encc.exe</outfile>
<jar>target/mavenproject1-1.0-SNAPSHOT.jar</jar>
<errTitle>encc</errTitle>
<classPath>
<mainClass>com.akathist.encc.Clui</mainClass>
<addDependencies>false</addDependencies>
<preCp>anything</preCp>
</classPath>
<jre>
<path>./win32/java</path>
</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>
</project>