Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TornadoFX unresolved JavaFx

I wanted to create a new project that should be a desktop application. For this purpose, I have selected Kotlin language and TornadoFX framework. I have installed the TornadoFXplugin and created a new Ttornadofx-gradle-project. The base setup made by Intellij was successful but I have encountered a problem. When I wanted to run the generated project it failed. The project cannot resolve the java fx. I have dug through the web and found nothing that would fix the problem. The error log that I receive after the failed build is:

error log

HAs anyone faces the same issue? How can I get rid of it?

I have installed the JDK 11 and set it up to the build config and I still receive the problem:

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

enter image description here

Is there a change that I have missed something in the middle?

like image 230
Patryk Jabłoński Avatar asked Jun 16 '19 17:06

Patryk Jabłoński


2 Answers

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:

enter image description here

like image 99
José Pereda Avatar answered Oct 14 '22 13:10

José Pereda


This answer is for those who wish to use Gradle Kotlin DSL.

An example of minimal build.gradle.kts:

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

application { mainClassName = "com.example.MyApp" }


repositories {
    mavenCentral()
    jcenter()
    maven("https://dl.bintray.com/kotlin/kotlin-eap")
}

dependencies {
    // Kotlin standard library
    implementation(kotlin("stdlib-jdk8"))
    // TornadoFX dependency
    implementation("no.tornado:tornadofx:1.7.20")
}

// JavaJX module to include
javafx { modules = listOf("javafx.controls", "javafx.fxml", "javafx.graphics") }

// Set Kotlin/JVM target versions
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions.jvmTarget = "11" // or higher
    kotlinOptions.languageVersion = "1.4"
}

// Be sure to use lates Gradle version
tasks.named<Wrapper>("wrapper") { gradleVersion = "6.6" }

For a full working example, check out GitHub repository

Please note that it also works with JDK 13 and 14

like image 37
kuza Avatar answered Oct 14 '22 15:10

kuza