vbyzjnlehi
vbyzjnlehi

Reputation: 347

How can I add --add-exports to a gradle application?

I'm working on a Java 11 application that uses the java module system. I need to use reflection to access an internal function of the JavaFX library. The java module containing that function doesn't export it, so I get the following error when I run the gradle build task:

import com.sun.javafx.tk.TKStage;
                     ^
  (package com.sun.javafx.tk is declared in module javafx.graphics, which does not export it to module com.sampleapp)

I believe that means I need to do --add-exports javafx.graphics/com.sun.javafx.tk=com.sampleapp, however, I'm not sure how to do this in gradle. I've tried adding it to the applicationDefaultJvmArgs, which didn't work.

application {
    ...

    applicationDefaultJvmArgs = listOf(
        "--add-exports javafx.graphics/com.sun.javafx.tk=com.sampleapp"
    )
}

I've also tried adding it to gradle.properties.kts and it didn't work.

org.gradle.jvmargs = listOf(
    "--add-exports javafx.graphics/com.sun.javafx.tk=com.sampleapp"
)

Upvotes: 4

Views: 3128

Answers (1)

vbyzjnlehi
vbyzjnlehi

Reputation: 347

I was able to solve the issue by adding:

tasks.withType<JavaCompile> {
    options.compilerArgs.addAll(arrayOf(
        "--add-exports", "javafx.graphics/com.sun.javafx.tk=com.sampleapp"
    ))
}

Upvotes: 5

Related Questions