Deploy JavaFX application using gradle 6.3, OpenJDK 14 and OpenJFX 14

戏子无情 提交于 2021-02-07 08:02:20

问题


I am developping a JavaFX application using gradle 6.3, OpenJDK 14 and OpenJFX 14 with IntelliJ and I want to deploy it (create a exe file).

I used to make JavaFX applications with JDK 8 under Netbeans 11 and the process was simple:

  1. Make the application
  2. Clean and build
  3. Look for the jar in the dist folder (which works perfectly when I click on it)
  4. convert it to exe using Launch4j
  5. Voilà!

Since I am migrating to OpenJDK 14, I wanted to make sure I can deploy the app I am making, so I created an application and everything works when I run it (in IntelliJ). I saw some tutorials on how to deploy a javafx app with gradle and I came up to this process:

  1. On the gradle window (on the top right of the screen), click on distribution > distZip
  2. Look for the generated ZIP file in build\distibutions and unzip it
  3. You will find a bin and lib folder: In the bin folder, I have found a .bat file of my project and when clicked on it, my app starts and everything works fine. In the lib folder, I have found all the jar files I used in my project plus my {project-name}.jar and when I click on it nothing happens.

I tried to run it using the command line and here's what I got:

    C:\Users\Admin\Documents\SmartDoc\build\distributions\SmartDoc-1.0\SmartDoc-1.0\lib>java -jar SmartDoc-1.0.jar
Table PATIENT created successfully !
Table DOSSIER_MEDICAL created successfully !
Table ORDONNANCE created successfully !
Table RENDEZ_VOUS created successfully !
Table SALLE_ATTENTE created successfully !
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
        at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: javafx.fxml.LoadException:
file:/C:/Users/Admin/Documents/SmartDoc/build/distributions/SmartDoc-1.0/SmartDoc-1.0/lib/SmartDoc-1.0.jar!/main.fxml:27

    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 com.smartdoc.gui.Launch.start(Launch.java:27)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:262)
        at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:54)
        at javafx.fxml.FXMLLoader$Element.applyProperty(FXMLLoader.java:520)
        at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:370)
        at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:332)
        at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:242)
        at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:775)
        at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
        ... 17 more
Caused by: java.lang.reflect.InvocationTargetException
        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:564)
        at com.sun.javafx.fxml.ModuleHelper.invoke(ModuleHelper.java:100)
        at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:258)
        ... 25 more
Caused by: java.lang.UnsupportedOperationException: Cannot resolve 'fa-user-plus'
        at org.kordamp.ikonli.javafx.IkonResolver.resolveIkonHandler(IkonResolver.java:62)
        at org.kordamp.ikonli.javafx.FontIcon.setIconLiteral(FontIcon.java:239)
        ... 31 more

The first 5 lines are the result of the application creating a derby database (works perfectly), but when it comes to running the app I got some exceptions. I also tried running with --module-path "C:\Program Files\Java\javafx-sdk-14.0.1\lib" --add-modules javafx.controls,javafx.fxml,javafx.base,javafx.graphics but I got the same exceptions.

There is 3 solutions which I know none of them:

  1. Solve this exceptions
  2. Try to convert that .bat file to .exe file
  3. A new solution

EDIT: build.gradle file:

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
}

group 'com.smartdoc'
version '1.0'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.calendarfx:calendar:11.8.3'
    compile 'com.calendarfx:view:11.8.3'
    compile 'org.kordamp.ikonli:ikonli-core:11.4.0'
    compile 'org.kordamp.ikonli:ikonli-javafx:11.4.0'
    compile 'org.kordamp.ikonli:ikonli-fontawesome-pack:11.4.0'
    compile 'com.jfoenix:jfoenix:9.0.9'
    compile group: 'org.apache.derby', name: 'derby', version: '10.15.2.0'
    compile group: 'org.apache.derby', name: 'derbyclient', version: '10.15.2.0'
    compile group: 'org.apache.derby', name: 'derbynet', version: '10.15.2.0'

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

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

mainClassName = "$moduleName/com.smartdoc.Main"

jar {
    manifest {
        attributes "Main-Class": "$mainClassName"
    }

    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

Thanks for your help!


回答1:


UPDATE 1: Making a working .jar file

So I tried some solutions to make my .jar file works, and I finally found THE ONE:

  1. Add to the top of your build.gradle this:
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0'
    }
}

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
    id 'com.github.johnrengelman.shadow' version '5.2.0'
}

apply plugin : 'com.github.johnrengelman.shadow'

(As you can see I used the Gradle Shadow Plugin)

  1. In the gradle window (top right of the intelliJ IDE), click distribution > shadowDistZip
  2. Go to build\distributions and you will find a ZIP file ({projectName}-shadow-{version}), unzip it.
  3. Open the folder and you will find bin and lib folders, go to lib folder for jar.
  4. You will find there your .jar that sould work like your expectations.

I am still looking how to make it .exe, so if anyone has a suggestion, welcome!

UPDATE 2: Making a .exe file using Launch4j

I have finally managed to create an .exe file from the jar I created previously: I have used Launch4j (yes, it works with Java > 11). Here's the simple steps:

  1. Download Launch4j first and install it (DONT RUN IT, IT WONT WORK)
  2. Go to Launch4j files and you will see a launch4j.jar file, run it and select Basic Tab
  3. Select the path where to save the output (make sure you write .exe at the end)
  4. Select the path where your .jar file is

  1. Now go to the JRE Tab, in the Min JRE version choose 11.0.1 to make sure it starts only with Java > 11, and in the Max JRE version choose the max Java version you want (in my case the last version is 14.0.1

  1. Now click on the cog on the top and save the config file wherever you want and Voilà! Your .exe file should be running now perfectly fine.

P.S.: You can add an icon, manifest file and many other stuff with Launch4j, I just explained the simpliest example ever just so you guys can make it work, the rest is up to you :)

If there is another solution, please share it with us, many people are suffering with JDK 14 with the executable files.

Thank you!



来源:https://stackoverflow.com/questions/61919197/deploy-javafx-application-using-gradle-6-3-openjdk-14-and-openjfx-14

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!