【问题标题】:How to package javafx application with Gradle?如何使用 Gradle 打包 javafx 应用程序?
【发布时间】:2020-04-23 14:01:33
【问题描述】:

我有使用 gradle 构建的 javafx 应用程序,并且我正在使用外部库 apache poixml 并且需要为其创建 jar 并且在使用 poixml 类时无法运行 我在 github 上尝试的示例项目:https://github.com/IslamAssem/HelloFX-Gradle

【问题讨论】:

    标签: gradle javafx package java-14


    【解决方案1】:

    我假设您使用单独下载的 OpenJFX14 jar 并将其包含到您的构建路径中。

    我会通过 Gradle 将 JavaFX 添加到您的依赖项中。

    
    dependencies {
        implementation 'org.apache.poi:poi:4.1.1'
        implementation 'org.apache.poi:poi-ooxml:4.1.1'
        testCompile group: 'junit', name: 'junit', version: '4.12'
    
    
        // here starts JavaFX
        implementation 'org.openjfx:javafx:14'
    
        compile 'org.openjfx:javafx-base:14'
        compile 'org.openjfx:javafx-graphics:14'
        compile 'org.openjfx:javafx-controls:14'
        compile 'org.openjfx:javafx-fxml:14'
        compile 'org.openjfx:javafx-swing:14'
        compile 'org.openjfx:javafx-media:14'
        compile 'org.openjfx:javafx-web:14'
    }
    
    

    那么我会建议您的 Main 不在从 Application 扩展的类中。

    您应该创建一个启动器。

    
    public class Launcher{
    
       public static void main(String[] args){
         Application.launch(HelloFX.class,args);
       }
    }
    
    

    然后用 JavaFX 构建一个 Jar...

    将此添加到您的 build.gradle:

    
    jar {
        manifest {
            attributes(
                    'Main-Class': 'your.main.package.Launcher' // replace with you main class
            )
        }
        from {
            configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
        }
    }
    

    这应该是交易。

    【讨论】:

    • 感谢它完美运行,但生成的 jar 非常大,大约 50mb,我只有一个在标签上打印文本的程序
    • @Islam Assem 原因可能是,您包含不需要的模块。例如 Jafaxf.media 等。只需包含您真正需要的内容。 apache Poi 本身也很大。
    • 好的,谢谢,我会开始学习 proguard 来获得这些类的红色
    猜你喜欢
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 2018-08-28
    • 2015-11-12
    • 2014-05-27
    相关资源
    最近更新 更多