Search code examples
gradlejavafxgradle-kotlin-dsl

JAVAFX environmental variable return null in build.gradle.kts


I am using following build.gradle.kts for JavaFX app

plugins {
    kotlin("jvm") version "1.4.30"
    id("application")
    id("org.openjfx.javafxplugin") version "0.0.9"
}

repositories {
    mavenCentral()
}

javafx {
    version = "11"
    modules("javafx.controls", "javafx.fxml")
    sdk = System.getenv("JAVAFX")
    if (sdk == null || sdk2.isBlank()) {
       throw InvalidUserDataException("JAVAFX environment variable is not set. JAVAFX = $sdk")

    }
    application {
        mainClass.set("example.Main")
        applicationName = "Main"
        applicationDefaultJvmArgs = listOf(
            "--module-path=${sdk}${File.separator}lib",
            "--add-modules=javafx.controls,javafx.fxml" )

        println("applicationDefaultJvmArgs:$applicationDefaultJvmArgs")
    }
}

dependencies {
    implementation(kotlin("stdlib"))
}

I have set environmental variable in .bashrc like below

export JAVAFX=$HOME/path/to/JavaFX/SDK

when I execute echo $JAVAFX I get the JavaFX SDK path but still I am getting null in build.gradle.kts

Tried restarting IntelliJ idea too, still the same.


Solution

  • Out-of-the-box, Gradle will spawn a Java process in the background. Typically this is the Gradle Daemon, but there may be more.

    So to ensure new environment variables are seen, it's best to kill any Java process that are running in the background. Depending on your system, open up task manager and kill and lingering Java process.