4

I have rewritten my little Java 8 project from simple jar to single module in Java 11. In past I was building jar with Gradle and it was compatible with Windows and Linux. Now I configured Gradle to build my module and create custom runtime image and it is working but only on Linux. My custom runtime image contains only Linux libraries. Is there possibility to build image for Windows on Linux? I know I could open my project on Windows and create there image but I would like to keep my project on single OS. Here is my Gradle build:

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.5'
}

group 'eu.sample'
version '2.0'


repositories {
    mavenCentral()
}

javafx {
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}

mainClassName = "$moduleName/eu.sample.app.Main"

def java_home = hasProperty('org.gradle.java.home') ? getProperty('org.gradle.java.home') : System.getenv('JAVA_HOME')
def fx_jmods = hasProperty('path.to.fx.mods') ? getProperty('path.to.fx.mods') : System.getenv('PATH_TO_FX_MODS')

dependencies {

}

task jlink(type: Exec) {
    dependsOn 'clean'
    dependsOn 'jar'

    workingDir 'build'

    if (java_home == null) {
        throw new RuntimeException("java_home is not defined.")
    }
    if (fx_jmods == null) {
        throw new RuntimeException("fx_jmods is not defined.")
    }
    commandLine "${java_home}/bin/jlink", '--module-path', "libs${File.pathSeparatorChar}${fx_jmods}",
            '--add-modules', "${moduleName}", '--output', "${moduleName}", '--strip-debug',
            '--compress', '2', '--no-header-files', '--no-man-pages'
}

I added to build.gradle lines, before fire jlinkWin task I run clean task:

task jlinkWin(type: Exec) {
    dependsOn 'clean'
    dependsOn 'jar'

    workingDir 'build'

    if (java_home == null) {
        throw new RuntimeException("java_home is not defined.")
    }
    if (fx_jmods == null) {
        throw new RuntimeException("fx_jmods is not defined.")
    }
    commandLine "${java_home}/bin/jlink", '--module-path', "/home/user1/Download/win-jdk-11.0.1/jmods${File.pathSeparatorChar}libs${File.pathSeparatorChar}${fx_jmods}",
            '--add-modules', "${moduleName}", '--output', "${moduleName}", '--strip-debug',
            '--compress', '2', '--no-header-files', '--no-man-pages'
}

Updated code above which creates custom runtime image for Windows but with out JavaFX libs.

GarryMoveOut
  • 377
  • 5
  • 19

1 Answers1

0

Download JDK (ex. openJDK) and openjfx jmods archive for platform you are interested (in this example Windows and Linux), extract them somewhere, next modify configuration file of gradle build.gradle.

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.5'
}

group 'eu.sample'
version '2.0'


repositories {
    mavenCentral()
}

javafx {
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}

mainClassName = "$moduleName/eu.sample.app.Main"

def lin_java_home = hasProperty('org.gradle.java.home') ? getProperty('org.gradle.java.home') : System.getenv('JAVA_HOME')
def lin_fx_jmods = hasProperty('linux.fx.mods') ? getProperty('linux.fx.mods') : System.getenv('PATH_TO_FX_MODS_LIN')

def win_java_home = hasProperty('windows.java.home') ? getProperty('windows.java.home') : System.getenv('JAVA_HOME_WIN')
def win_fx_jmods = hasProperty('windows.fx.mods') ? getProperty('windows.fx.mods') : System.getenv('PATH_TO_FX_MODS_WIN')

dependencies {

}

task jlink(type: Exec) {
    dependsOn 'clean'
    dependsOn 'jar'

    workingDir 'build'

    if (lin_java_home == null) {
        throw new RuntimeException("java_home is not defined.")
    }
    if (lin_fx_jmods == null) {
        throw new RuntimeException("fx_jmods is not defined.")
    }
    commandLine "${lin_java_home}/bin/jlink", '--module-path', "libs${File.pathSeparatorChar}${lin_fx_jmods}",
            '--add-modules', "${moduleName}", '--output', "${moduleName}", '--strip-debug',
            '--compress', '2', '--no-header-files', '--no-man-pages'
}

task jlinkWin(type: Exec) {
    dependsOn 'clean'
    dependsOn 'jar'

    workingDir 'build'

    if (win_java_home == null) {
        throw new RuntimeException("java_home is not defined.")
    }
    if (win_fx_jmods == null) {
        throw new RuntimeException("fx_jmods is not defined.")
    }
    commandLine "${lin_java_home}/bin/jlink", '--module-path', 
            "${win_java_home}/jmods${File.pathSeparatorChar}libs${File.pathSeparatorChar}${win_fx_jmods}",
            '--add-modules', "${moduleName}", '--output', "${moduleName}", '--strip-debug',
            '--compress', '2', '--no-header-files', '--no-man-pages'
}

And add or modify gradle.properties, add paths to JDK's and openjfx jmods:

org.gradle.java.home=/usr/java/jdk-11/
windows.java.home=/home/user1/Download/win-jdk-11.0.1/

linux.fx.mods=/usr/lib64/javafx-jmods-11.0.1
windows.fx.mods=/home/user1/Download/javafx-jmods-11.0.1/

Finally fire gradle task jlink to build image for Linux or jlinkWin for Windows

GarryMoveOut
  • 377
  • 5
  • 19