java - 在 gradle 中将 JRE 与 launch4j 应用程序捆绑在一起

标签 java gradle launch4j

背景

我正在使用 edu.sc.seis.launch4j 插件通过 gradle 构建脚本构建可分发应用程序。我正在尝试使用捆绑的 JRE 来生成这些内容。

这是gradle脚本

plugins {
    id 'org.jetbrains.intellij' version '0.3.7'
    id 'java'
    id 'edu.sc.seis.launch4j' version '2.4.4'
}

group 'worldbuilders'
version '0.4.4-SNAPSHOT'

apply plugin: 'application'

sourceCompatibility = 1.8

repositories {
    jcenter()
    mavenCentral()
}

mainClassName = 'hello.HelloWorld'

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

intellij {
    version '2017.3.5'
}

launch4j {
    bundledJrePath = "jre" //my jre to use is in the projectRoot/jre folder but I think that's not what this parameter is for anyway
    mainClassName = 'hello.HelloWorld'
    bundledJre64Bit = true
    //icon = "${projectDir}/icons/myApp.ico"
}

令人沮丧的是,这会创建一个运行的应用程序(由 gradle 任务 createExe 创建的 exe),但显然没有捆绑在其中/旁边的 JRE,大概是因为它运行是因为它回退到使用系统 jre,这使得测试事情很困难。如果我将故意损坏的 jre 放在/jre/中,它似乎仍然可以运行,这更加令人困惑

问题

如何将 JRE 与使用 gradle-launch4j 插件创建的 exe 分发包捆绑在一起? (这实际上是由exe使用而不是使用系统jre)

其他信息

插件创建的调试 XML(由 launch4j 使用):

使用命令gradle createExe -Pl4j-debug创建

<?xml version='1.0' encoding='UTF-8'?>
<launch4jConfig>
  <dontWrapJar>false</dontWrapJar>
  <headerType>gui</headerType>
  <jar>lib/onemillionworlds-0.4.4-SNAPSHOT.jar</jar>
  <outfile>onemillionworlds.exe</outfile>
  <errTitle></errTitle>
  <cmdLine></cmdLine>
  <chdir>.</chdir>
  <priority>normal</priority>
  <downloadUrl>http://java.com/download</downloadUrl>
  <supportUrl></supportUrl>
  <stayAlive>false</stayAlive>
  <restartOnCrash>false</restartOnCrash>
  <manifest></manifest>
  <icon></icon>
  <classPath>
    <mainClass>hello.HelloWorld</mainClass>
    <cp>lib\onemillionworlds-0.4.4-SNAPSHOT.jar</cp>
    <cp>lib\tools.jar</cp>
    <cp>lib\jme3-lwjgl-3.2.0-stable.jar</cp>
    <cp>lib\jme3-desktop-3.2.0-stable.jar</cp>
    <cp>lib\jme3-core-3.2.0-stable.jar</cp>
    <cp>lib\lwjgl-2.9.3.jar</cp>
    <cp>lib\lwjgl-platform-2.9.3-natives-windows.jar</cp>
    <cp>lib\lwjgl-platform-2.9.3-natives-linux.jar</cp>
    <cp>lib\lwjgl-platform-2.9.3-natives-osx.jar</cp>
    <cp>lib\jinput-2.0.5.jar</cp>
    <cp>lib\jutils-1.0.0.jar</cp>
    <cp>lib\jinput-platform-2.0.5-natives-linux.jar</cp>
    <cp>lib\jinput-platform-2.0.5-natives-windows.jar</cp>
    <cp>lib\jinput-platform-2.0.5-natives-osx.jar</cp>
  </classPath>
  <jre>
    <path>jre</path>
    <bundledJre64Bit>true</bundledJre64Bit>
    <bundledJreAsFallback>false</bundledJreAsFallback>
    <minVersion>1.8.0</minVersion>
    <maxVersion></maxVersion>
    <jdkPreference>jdkOnly</jdkPreference>
    <runtimeBits>64/32</runtimeBits>
  </jre>
  <versionInfo>
    <fileVersion>0.0.0.1</fileVersion>
    <txtFileVersion>unspecified</txtFileVersion>
    <fileDescription>onemillionworlds</fileDescription>
    <copyright>unknown</copyright>
    <productVersion>0.0.0.1</productVersion>
    <txtProductVersion>unspecified</txtProductVersion>
    <productName>onemillionworlds</productName>
    <companyName></companyName>
    <internalName>onemillionworlds</internalName>
    <originalFilename>onemillionworlds.exe</originalFilename>
    <trademarks></trademarks>
    <language>ENGLISH_US</language>
  </versionInfo>
</launch4jConfig>

最佳答案

务实的回答

以 2 年的距离来看这个问题,我意识到我的问题实际上是“我希望 Launch4J 的行为与 bat 文件完全相同”。对于 Linux 版本,我使用了相当于 Linux 的 bat、sh 文件,回想起来,我应该对 Windows 版本执行相同的操作。

字面答案

launch4j的bundledJrePath配置告诉launch4j在哪里找到JRE,而不是把它放在哪里,你必须单独把它放在那里。如果 launch4j 找不到它,它将使用已安装的 JRE(或下载一个)。

我的相关 Gradle 配置最终是这样的

launch4j { //used for windows
    mainClassName = 'mygame.Main'
    bundledJrePath = 'jre'
    bundledJre64Bit = true
    jreMinVersion = '11'
}

task packageExecutableDistribution(type: Zip) {
    archiveName = "oneMillionWorlds.zip"
    destinationDir = file("$buildDir/distExecutable")

    from "$buildDir/launch4j"
}

task addJreToDistributable(type: Copy) {
    from zipTree("resources/desktop-deployment/OpenJDK11U-jre_x64_windows_hotspot_11.0.4_11.zip")
    destinationDir = file("$buildDir/launch4j")
}

packageExecutableDistribution.dependsOn createExe
packageExecutableDistribution.dependsOn addJreToDistributable

我的文件夹结构最终是这样的

enter image description here

哪里可以找到 JRE

由于现在找到 JRE 变得有点困难,您可以在 https://adoptium.net/en-GB/temurin/releases/ 找到 JRE 的采用版本。或者,如果您希望能够以编程方式获取 JRE,您可以从 https://api.adoptium.net/v3/assets/feature_releases/[DESIRED_VERSION]/ga?image_type=jre< 找到 json 格式的链接,例如https://api.adoptium.net/v3/assets/feature_releases/19/ga?image_type=jre

或者,您可以从 JDK 开始,并使用 JLink 为您的用例生成最小的 JRE

关于java - 在 gradle 中将 JRE 与 launch4j 应用程序捆绑在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52150565/

相关文章:

java - 如何在java android中构造多个对象和JSON数组

java - 在 session 中保存模型

gradle - Gradle Kotlin DSL 中的 kotlin ("...") 方法是什么?

gradle - Groovy:多次声明字段属性

java - 有没有办法让 Launch4j 3.12 使用捆绑的 OpenJDK 而不是 Oracle JRE?

java - 我可以在 Java 泛型中传递复杂类型结构吗?

java - 如何获取带标签的pdf中标签的页码

android - 应用程序不会在启用即时运行的 gradle 2.14.1 的新 Android Studio 2.2 上执行

java - Launch4j:This application requires a Java Runtime Environment 1.8.0_161 using maven?

build - Launch4j/windres : how to set paths correctly?