首页 > 解决方案 > 使用 IntelliJ、OpenJFX 和 Gradle 构建 JAR 文件

问题描述

在 2021 年,使用 OpenJFX 在 IntelliJ 中使用 Gradle 创建可执行 jar 文件的最佳方法是什么?我发现的每个解决方案都非常复杂,并且使用了变通方法。这是我当前的 build.gradle 文件

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

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

repositories {
    mavenCentral()
}

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

mainClassName = 'com.company.crawl.main.Main'

group 'com.company'
version '1.0-SNAPSHOT'

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
    compile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '9.2.0.jre15'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
    compile group: 'org.apache.commons', name: 'commons-csv', version: '1.8'
}

jar {
    from configurations.compile.collect {zipTree it}
    manifest.attributes "Main-Class": "com.company.crawl.main.Main"
}

如果我运行 gradle :jar,jar 文件包含依赖项,但不包含 openjfx sdk。使用 openjfx sdk 运行 jar 需要做什么?

更新 1

将jar路径更改为:

jar {
    manifest {
        attributes 'Main-Class': 'com.company.crawl.main.Main'
    }
    from {
        configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)}
    }
}

重建 jar 后,出现错误:“JavaFX 运行时组件丢失”。我不知道为什么,因为在 jar 文件中包含 javafx 文件夹。之后,我添加了 VM 选项:

--module-path /path/to/javafx-sdk/lib --add-modules=javafx.controls,javafx.fxml

jar 文件运行。那么我做错了什么?

标签: javagradleintellij-ideaopenjfx

解决方案


推荐阅读