4

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?

Patryk Jabłoński
  • 697
  • 10
  • 29

3 Answers3

8

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

José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • 1
    Hey. This helped. Unfortunately, I have run into another error. `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` is there a chance that you know how to fix this? – Patryk Jabłoński Jun 23 '19 at 20:06
  • 1
    You need to use JDK 11 or 12, the error indicates that you are using JDK 1.8. – José Pereda Jun 23 '19 at 20:08
  • I have installed the JDK 11 and set it up as a runtime in the build configuration of the Intelij IDE. I have no idea where to go now :(. The screenshot is applied to the main question post. – Patryk Jabłoński Jun 24 '19 at 18:35
  • Make sure your project is set to JDK 11: Project Structure -> project (SDK and language level), and also the gradle settings -> JVM is set to JDK 11. Then refresh the gradle project. – José Pereda Jun 24 '19 at 18:42
  • Thank you very much for help – Patryk Jabłoński Jun 24 '19 at 18:56
1

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

kuza
  • 2,761
  • 3
  • 22
  • 56
0

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