nus-cs2103-AY2324S1 / forum

10 stars 0 forks source link

Resolving JavaFX Cross-Platform Issues with Gradle JavaFX Plugin #173

Open woojiahao opened 11 months ago

woojiahao commented 11 months ago

Preface

There has been several issues open about not being able to run the JAR cross-platform for various reasons, most occurring because the Gradle setup for JavaFX was using the following:

plugins {
    // ...
    id 'checkstyle'
    id 'org.openjfx.javafxplugin' version '0.0.13'
}

javafx {
    version = '17.0.7'
    modules = ['javafx.base', 'javafx.controls', 'javafx.fxml', 'javafx.graphics']
}

The error is a QuantumReader error.

Solution

I was digging through the official OpenJFX documentation to find a potential fix that continued using the (formerly) second method of importing JavaFX in Gradle:

Screenshot 2023-09-22 at 09 48 38

This is my build.gradle now. I've tested out the JAR on a Windows ARM build emulated on Mac M1 Pro machine:

plugins {
    // ...
    id 'checkstyle'
    id 'org.openjfx.javafxplugin' version '0.0.13'
}

dependencies {
    testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.10.0'
    testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.10.0'
    implementation group: 'com.google.code.gson', name: 'gson', version: '2.10.1'

    // Used for loops to clean up the imports
    def platforms = ["win", "linux", "mac"]
    def javafxDependency = ["javafx-graphics", "javafx-controls", "javafx-fxml"]

    for (plt in platforms) {
        for (dep in javafxDependency) {
            runtimeOnly "org.openjfx:$dep:$javafx.version:$plt"
        }
    }
}

// ...

javafx {
    version = '17.0.7'
    modules = ['javafx.base', 'javafx.controls', 'javafx.fxml', 'javafx.graphics']
}

I am currently setting up VMs (both emulated and virtualized) on a Macbook Pro 14 (M1 Pro) to test various platforms. Would be great if others could try this fix too and see if it resolves their cross-platform issue.

OS Architecture Works?
MacOS ARM (M1 Pro)
Windows ARM (Virtualized)
Windows x86 (Live Machine)
Linux x86 (Ubuntu Live Machine)

Note: I still have the Gradle checkstyle plugin so there is no need to delete it when using this method

damithc commented 11 months ago

Thanks for exploring this fix @woojiahao 💯 Let's hope this can solve the problem for those using using the second method.

woojiahao commented 11 months ago

Good news, I've managed to test this fix across all OSes on both ARM and x86 and it works for all!