使用Gradle和JavaFX创建一个使用自定义JVM参数的可运行jar

bpzcxfmw  于 2023-01-09  发布在  Java
关注(0)|答案(1)|浏览(105)
    • 上下文:**我正在Intellij上构建一个模块化(Java 11-JavaFX 11)项目,该项目使用Gradle 5.4.1,并添加了一些外部库(JFoenix、ControlsFX、ShadowJar ...)。
      **Problem:**The library JFoenix(a JavaFX library) doesn't really support Java11, it needs some custom 'applicationDefaultJvmArgs' to run (see gradle.build file below). When I compile and run the project it works fine, but when trying to create a FatJar(using ShadowJar) or a runtime image (see https://openjfx.io/openjfx-docs/ ) it shows an error (because the JFoenix component JFXDatePicker needs those 'applicationDefaultJvmArgs to work')
    • 代码:**

build.gradle:

plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
id 'com.github.johnrengelman.shadow' version "5.0.0"
id "org.beryx.jlink" version "2.9.4"
}

group 'yayarh'
version '1.0-SNAPSHOT'

sourceCompatibility = '11'
targetCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    implementation group: 'com.jfoenix', name: 'jfoenix', version: '9.0.9'
    implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.19'
    implementation group: 'org.controlsfx', name: 'controlsfx', version: '11.0.0'
}

mainClassName = "yayarh.Launcher"

application {
    'yayarh.Launcher'
    applicationDefaultJvmArgs = ["--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",
                                 "--add-exports=javafx.controls/com.sun.javafx.scene.control=com.jfoenix",
                                 "--add-exports=javafx.base/com.sun.javafx.binding=com.jfoenix",
                                 "--add-exports=javafx.graphics/com.sun.javafx.stage=com.jfoenix",
                                 "--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",
                                 "--add-exports=javafx.base/com.sun.javafx.event=com.jfoenix"]
}

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

jlink {
    launcher {
        name = 'launcher'
    }
}

jar {
    manifest {
        attributes "Main-Class": 'yayarh.Launcher'
    }
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

javafx {
    version = '11'
    modules = ['javafx.controls', 'javafx.fxml']
}

module-info.java:

module sifana {
requires javafx.fxml;
requires javafx.controls;
requires java.desktop;
requires com.jfoenix;
requires mysql.connector.java;
requires java.sql;
requires org.controlsfx.controls;

opens yayarh to javafx.fxml;

exports yayarh;

}
运行FatJar时显示的错误:

file:/E:/IntellijProjects/SifanaManager/build/libs/sifana-1.0-SNAPSHOT-all.jar!/yayarh/OmraN1.fxml:19

        at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
        at yayarh.HomeController.goToOmraScene(HomeController.java:20)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:567)
        at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:567)
        at com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
        at com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
        at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Node.fireEvent(Node.java:8879)
        at javafx.scene.control.Button.fire(Button.java:200)
        at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
        at com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
        at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
        at javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
        at javafx.scene.Scene.processMouseEvent(Scene.java:1849)
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
        at com.sun.glass.ui.View.handleMouseEvent(View.java:556)
        at com.sun.glass.ui.View.notifyMouse(View.java:942)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
        at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.lang.NullPointerException
        at com.jfoenix.controls.JFXDatePicker.initialize(JFXDatePicker.java:84)
        at com.jfoenix.controls.JFXDatePicker.<init>(JFXDatePicker.java:62)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
        at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:166)
        at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:404)
        at java.base/java.lang.Class.newInstance(Class.java:590)
        at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1019)
        at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
        at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
        ... 67 more
    • 所需的解决方案:*我想将我的项目打包为包含" applicationDefaultJvmArgs *"的jar或可执行文件(它只能在Windows上运行),以便JFoenix组件可以正常工作而不会出错。
    • 注意:**这是我在这里遇到的第一个问题+我对JavaFX和将项目导出到jar/exe没有太多经验。
c6ubokkw

c6ubokkw1#

如果您遇到同样的问题,请查看Openjfx官方文档了解如何使用JavaFX和Gradle创建运行时映像,然后可以在jlink块中添加自定义jvm参数:

application {
    'yayarh.Launcher'
    applicationDefaultJvmArgs = ["--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",
                                 "--add-exports=javafx.controls/com.sun.javafx.scene.control=com.jfoenix",
                                 "--add-exports=javafx.base/com.sun.javafx.binding=com.jfoenix",
                                 "--add-exports=javafx.graphics/com.sun.javafx.stage=com.jfoenix",
                                 "--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",
                                 "--add-exports=javafx.base/com.sun.javafx.event=com.jfoenix"]
}

   jlink {
    ...
    launcher {
        name = 'Your app name'
        jvmArgs = applicationDefaultJvmArgs as List<String>
     }
    }

相关问题