Bundle in JavaFX when you run the executable from a Gradle project?

前端 未结 1 1297
醉酒成梦
醉酒成梦 2020-12-22 02:04

Over the past few days I\'ve been tearing what\'s left of my hair out trying to get to the Holy Grail: Gradle + Java 11 + JavaFX + writing all my app and testing code in Gro

相关标签:
1条回答
  • 2020-12-22 02:24

    Slaw and cfrick came up with the answer to my question in their comments. I thought this might come in useful for some.

    If you want the JavaFX modules on the class-path then create a separate main class which does not extend Application and invoke Application.launch(YourApp.class, args) from the main method.

    cfrick's example stripped-down working Gradle project is here. I added the following line:

    installDist{}
    

    ... this makes a new task available: ./gradlew installdist will then produce an executable under build/install/.

    In case cfrick's github page goes away, build.gradle is like this:

    plugins {
        id 'groovy'
        id 'application'
        id 'org.openjfx.javafxplugin' version '0.0.8'
    }
    
    repositories {
        jcenter()
    }
    
    javafx {
        version = "11.0.2"
        modules = [ 'javafx.controls' ]
    }
    
    dependencies {
        implementation 'org.codehaus.groovy:groovy:3.+'
    }
    
    application {
        mainClassName = 'ofx.App'
    }
    
    installDist{} // my addition
    

    and src/main/groovy/ofx/App.groovy is like this:

    package ofx
    
    import javafx.application.Application
    import javafx.scene.Scene
    import javafx.scene.control.Label
    import javafx.scene.layout.StackPane
    import javafx.stage.Stage
    
    class App {
        static void main(String[] args) {
            Application.launch(HelloFx, args)
        }
    }
    
    class HelloFx extends Application {
    
        @Override
        void start(Stage stage) {
            def javaVersion = System.getProperty("java.version")
            def javafxVersion = System.getProperty("javafx.version")
            Label l = new Label("Hello, JavaFX $javafxVersion, running on Java $javaVersion.")
            Scene scene = new Scene(new StackPane(l), 640, 480)
            stage.setScene(scene)
            stage.show()
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题