TornadoFX unresolved JavaFx

六眼飞鱼酱① 提交于 2019-12-06 08:22:33

It looks like you are running the TornadoFX project with Java 11 or 12.

It also looks like the TornadoFX plugin is intended for Java 1.8, but it is not advised what to do with Java 11+.

Since Java 11, JavaFX is no longer part of the JDK.

You can read all about getting JavaFX as a third party dependency into your project here: https://openjfx.io/openjfx-docs/, and since you are using Gradle, this section will be helpful: https://openjfx.io/openjfx-docs/#gradle.

I've just installed the Tornado plugin, and created a project, using JDK 12.0.1. I've also updated the gradle-wrapper.properties file to use Gradle 5.3-bin as the default 4.4 doesn't work with Java 11+.

If I run it, I get the same errors:

e: /.../src/main/kotlin/com/example/demo/app/Styles.kt: (3, 8): \
Unresolved reference: javafx
e: /.../src/main/kotlin/com/example/demo/app/Styles.kt: (18, 13): \
Cannot access class 'javafx.scene.text.FontWeight'. Check your module classpath for missing or conflicting dependencies
...

Basically these errors indicate that JavaFX is not found. The Tornado plugin wasn't expecting this.

Solution

There is an easy solution to make this work: add the JavaFX gradle plugin to the build, so it deals with the JavaFX part.

According to the plugin's repository, all you need to do is edit the build.gradle file and add:

buildscript {
    ext.kotlin_version = "1.2.60"
    ext.tornadofx_version = "1.7.17"
    ext.junit_version = "5.1.0"

    repositories {
        mavenLocal()
        mavenCentral()
        maven {
            setUrl("https://plugins.gradle.org/m2/")
        }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.junit.platform:junit-platform-gradle-plugin:1.1.0"
// Add JavaFX plugin:
        classpath 'org.openjfx:javafx-plugin:0.0.7'
    }
}

apply plugin: "kotlin"
apply plugin: "application"
apply plugin: "org.junit.platform.gradle.plugin"
// Apply JavaFX plugin:
apply plugin: 'org.openjfx.javafxplugin'

// Add the JavaFX version and required modules:
javafx {
    version = "12.0.1"
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}
...

And this is it, refresh your project, the IDE should recognize all the JavaFX classes.

If you modify the default MainView.kt like:

class MainView : View("Hello TornadoFX \n with JavaFX " 
        + System.getProperty("javafx.version")) {
    override val root = hbox {
        label(title) {
            addClass(Styles.heading)
        }
    }
}

you should be able to run it:

i'm recieved this error when download Kodein-Samples and trying to run tornadofx sample under Java11/12 and JavaFX13.

java.lang.UnsupportedClassVersionError: org/openjfx/gradle/JavaFXPlugin has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

The solution was is quite simple: i'm only comment another modules in settings.gradle (because the error occurred in some other module). Unfortunately, after the launch the application generates an error when trying to edit the record. I haven't dealt with it yet. so my build.gradle.kts looks like this:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

val kodeinVersion: String by rootProject.extra

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

repositories {
    jcenter()
    maven(url = "https://dl.bintray.com/kodein-framework/Kodein-DI/")
}

application {
    mainClassName = "org.kodein.samples.di.tornadofx.KodeinApplication"
}

javafx {
    version = "13"
    modules = mutableListOf("javafx.controls", "javafx.fxml", "javafx.base", "javafx.media")
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = JavaVersion.VERSION_11.toString()
}

dependencies {
    implementation(kotlin("stdlib-jdk8"))
    implementation("no.tornado:tornadofx:1.7.19")

    implementation("org.kodein.di:kodein-di-generic-jvm:$kodeinVersion")
    implementation("org.kodein.di:kodein-di-conf:$kodeinVersion")
    implementation("org.kodein.di:kodein-di-framework-tornadofx-jvm:$kodeinVersion")
}

i made fork for this example with changes: https://github.com/ibelozor/Kodein-Samples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!