edvin / fxlauncher

Auto updating launcher for JavaFX Applications
Apache License 2.0
715 stars 107 forks source link

fxlauncher and TornadoFX: java.lang.InstantiationException #114

Closed dom3k closed 6 years ago

dom3k commented 6 years ago

At the beginning I want to thank you for your contribution to fxlauncher and TornadoFX.

Unfortunately, I have a problem with the correct configuration.

After download and run fxlauncher from fx.appsdev.pl/pankdev

I have got this error:

WARNING: Error creating app class
java.lang.InstantiationException: pl.pankdev.MainKt
        at java.lang.Class.newInstance(Unknown Source)
        at fxlauncher.Launcher$1.lambda$createApplication$1(Launcher.java:49)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: pl.pankdev.MainKt.<init>()
        at java.lang.Class.getConstructor0(Unknown Source)
        ... 10 more

kwi 28, 2018 4:37:09 PM fxlauncher.Launcher$1 reportError
WARNING: Error during Application Environment Prepare phase
java.lang.NullPointerException
        at fxlauncher.Launcher.launchAppFromManifest(Launcher.java:161)
        at fxlauncher.Launcher.lambda$start$0(Launcher.java:150)
        at java.lang.Thread.run(Unknown Source)

I have no idea what I'm doing wrong. When I run prue JavaFx class without tornadoFx and Kotlin everything works fine.

Could you help me? Thanks.

My gradle.build

group 'pl.pankdev'
version '1.0'

buildscript {
    ext.kotlin_version = '1.2.40'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'no.tornado:fxlauncher-gradle-plugin:1.0.18'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin'

apply plugin: 'no.tornado.fxlauncher'
apply plugin: 'application'

fxlauncher {
    applicationVendor 'pankdev'
    applicationUrl 'https://fx.appsdev.pl/pankdev'
    applicationMainClass 'pl.pankdev.MainKt'
    acceptDowngrade false
}

mainClassName = 'pl.pankdev.MainKt'

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    compile 'no.tornado:tornadofx:1.7.15'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

}

sourceCompatibility = 1.8

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

sourceSets {
    main.kotlin.srcDirs += 'src/main/kotlin/'
    main.java.srcDirs += 'src/main/java/'
}
jar {
//    baseName 'aplikacja'

    manifest {
        attributes(
        'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
        'Main-Class': 'pl.pankdev.MainKt'
        )
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

main.kt

package pl.pankdev

import javafx.application.Application
import tornadofx.App

class MyApp:App(MainView::class)

fun main(args: Array<String>) {
    println("Witaj")

    Application.launch(MyApp::class.java, *args)
}

MainView.kt

package pl.pankdev

import javafx.geometry.Insets
import javafx.scene.text.Font
import tornadofx.View
import tornadofx.hbox
import tornadofx.label
import tornadofx.vbox

class MainView : View() {

    override val root = vbox {

        title = "Application v1.0"
        padding = Insets(10.0)
        setPrefSize(400.0, 400.0)

        hbox {
            label {
                text = "Hello"
                font = Font(20.0)
            }
        }
    }
}
dom3k commented 6 years ago

I forgot to add that runing directly https://fx.appsdev.pl/pankdev/fxldemo-gradle-1.0.jar works fine. I add my zipped project to https://fx.appsdev.pl/fxldemo-gradle/fxldemo-gradle.zip

dom3k commented 6 years ago

Ok, after thousand tries, finally i found solution. It is my main class:


class Main() :App(MainView::class) {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            println("Start App")

            Application.launch(Main::class.java,*args)
        }
    }
}

because of:

Caused by: java.lang.NoSuchMethodException: pl.pankdev.MainKt.<init>()
        at java.lang.Class.getConstructor0(Unknown Source)
        ... 10 more

I am happy now ;)

edvin commented 6 years ago

This is not the way to go. You're supposed to mention the JavaFX Application subclass you created, in this case pl.pankdev.MyAppKt. You don't need a main method, java already knows how to start JavaFX Applications directly. Kick out your main function and reference the app class directly.

dom3k commented 6 years ago

Thank you. Now everything is clear for me. All the best for you.

edvin commented 6 years ago

Great :)