簡體   English   中英

使用 gradle 6.3、OpenJDK 14 和 OpenJFX 14 部署 JavaFX 應用程序

[英]Deploy JavaFX application using gradle 6.3, OpenJDK 14 and OpenJFX 14

我正在使用 gradle 6.3、OpenJDK 14 和 OpenJFX 14 和 IntelliJ 開發 JavaFX 應用程序,我想部署它(創建一個 exe 文件)。

我曾經在 Netbeans 11 下使用 JDK 8 制作 JavaFX 應用程序,過程很簡單:

  1. 制作應用程序
  2. 清潔和建造
  3. 在 dist 文件夾中尋找 jar (當我點擊它時它工作得很好)
  4. 使用 Launch4j 將其轉換為 exe
  5. 瞧!

由於我正在遷移到 OpenJDK 14,因此我想確保我可以部署我正在制作的應用程序,因此我創建了一個應用程序,並且在我運行它時一切正常(在 IntelliJ 中)。 我看到了一些關於如何使用 gradle 部署 javafx 應用程序的教程,我想到了這個過程:

  1. 在 gradle window(在屏幕右上角)上,單擊分布 > distZip
  2. 在 build\distibutions 中查找生成的 ZIP 文件並解壓
  3. 你會找到一個binlib文件夾:在bin文件夾中,我找到了我的項目的.bat文件,當點擊它時,我的應用程序啟動並且一切正常。 lib文件夾中,我找到了我在項目中使用的所有 jar 文件以及我的 {project-name}.jar,當我點擊它時,什么也沒有發生。

我嘗試使用命令行運行它,這就是我得到的:

    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

前 5 行是應用程序創建 derby 數據庫的結果(運行良好),但在運行應用程序時出現了一些異常。 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我有同樣的例外。

有 3 個我不知道的解決方案:

  1. 解決這個異常
  2. 嘗試將該.bat文件轉換為.exe文件
  3. 一種新的解決方案

編輯: build.gradle 文件:

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) }
    }
}

謝謝你的幫助!

更新1:制作一個working.jar文件

所以我嘗試了一些解決方案來使我的.jar文件有效,我終於找到了一個:

  1. 添加到您的build.gradle的頂部:
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'

(如您所見,我使用了Gradle 影子插件

  1. 在 gradle window(intelliJ IDE 的右上角)中,單擊分布 > shadowDistZip
  2. Go構建\發行版,你會找到一個 ZIP 文件({projectName}-shadow-{version}),解壓縮它。
  3. 打開文件夾,你會發現binlib文件夾,go 到 jar 的lib文件夾。
  4. 您會在那里找到您的.jar ,它的工作原理與您的期望一樣。

我還在尋找如何制作它.exe,所以如果有人有建議,歡迎!

更新 2:使用 Launch4j 制作 a.exe 文件

我終於設法從我之前創建的 jar 創建了一個.exe文件:我使用了 Launch4j (是的,它適用於 Java > 11)。 以下是簡單的步驟:

  1. 首先下載Launch4j並安裝它(不要運行它,它不會工作)
  2. Go 到 Launch4j 文件,你會看到一個launch4j.jar文件,運行它和 select基本選項卡
  3. Select 保存 output 的路徑(確保最后寫入.exe)
  4. Select .jar文件所在的路徑

快速示例

  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

JRE 示例

  1. 現在單擊頂部的齒輪並將配置文件保存在您想要的任何位置,然后瞧。 Your.exe 文件現在應該可以正常運行了。

PS:您可以使用 Launch4j 添加圖標、清單文件和許多其他內容,我只是解釋了最簡單的示例,以便你們可以使其工作,rest 由您決定:)

如果有其他解決方案,請與我們分享,很多人都在為 JDK 14 的可執行文件而苦惱。

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM