【问题标题】:not recoginzed Resource only in out folder and gradle run仅在 out 文件夹和 gradle 运行中无法识别资源
【发布时间】:2018-12-06 20:17:07
【问题描述】:

我在使用 gradle 运行我的简单 javafx 应用程序时遇到问题。

我有空白的标准 sample.fxml 文件:

<GridPane fx:controller="de.hhn.se.pmt.flowertours.controllers.Controller"
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
</GridPane>

在我的 src 文件夹中,Intellij 和 gradle 不要抱怨。 gradle build 成功执行,但 gradle run 总是在 xmlns:fx="http://javafx.com/fxml" 行失败。

我的 build.gradle 看起来像这样:

plugins {
    id 'java'
    id 'checkstyle'
    id 'jacoco'
    id 'com.github.spotbugs' version '1.6.5'
}

apply plugin: 'application'
group 'de.hhn.se.pmt'
version '1.0-SNAPSHOT'

mainClassName  = 'de.hhn.se.pmt.flowertours.Main'

allprojects{
    compileJava {
        options.encoding = 'UTF-8'
        sourceCompatibility '1.8'
        targetCompatibility '1.8'
    }

    repositories {
        google()
        jcenter()
        mavenLocal()
        mavenCentral()

    }
}

repositories {
    flatDir{
        dirs "libs"
    }
}

dependencies {
    compile project (':gen')

    compile name: 'orm'
    compile name: 'jfxrt'

    compile group: 'com.jfoenix', name: 'jfoenix', version: '1.2.0'
    //logging with slf4j
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.24'
    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
    //use JUnit 5
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.0'
    testCompile 'org.assertj:assertj-core:3.11.1'

}

添加的库 omr 和子项目 gen 尚未使用,不应执行(因此它们不应该是问题)。 jfxrt 库试图修复它,但没有必要。

我的 Main.java 看起来像这样:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }

    /**
     * "main" method that starts the program.
     *
     * @param args start parameter
     */
    public static void main(String[] args) {
        launch(args);
    }
}

看起来在运行时缺少 javafx 库,但包含 jfxrtruntime name: 'jfxrt' 并不能解决问题。

我做错了什么?这是我必须在 Intellij 中解决的问题吗?

【问题讨论】:

  • Is it a problem I have to fix in Intellij? 如果使用 Gradle 从命令行失败,则 build.gradle 配置中存在问题。请发布您得到的实际错误和您使用的 JDK。

标签: java gradle intellij-idea javafx


【解决方案1】:

为了清楚起见,我没有找到任何真正好的答案 - javafx 和 gradle 不是朋友,intellij 的实现版本并没有让它变得更容易。

从 gradle 插件 application 创建的 Jar-File 不包含所需的 javafx 文件 - 您需要包含来自 openjfx 的新依赖项才能实际构建运行的 javafx jar 文件。

这是依赖项的设置,并不是真正需要的:

def currentOS = org.gradle.internal.os.OperatingSystem.current()
def platform
if (currentOS.isWindows()) {
    platform = 'win'
} else if (currentOS.isLinux()) {
    platform = 'linux'
} else if (currentOS.isMacOsX()) {
    platform = 'mac'
}

这已添加到我的依赖项中:

compile "org.openjfx:javafx-base:11:${platform}"
compile "org.openjfx:javafx-graphics:11:${platform}"
compile "org.openjfx:javafx-controls:11:${platform}"
compile "org.openjfx:javafx-fxml:11:${platform}"

这些附加设置包括您的 jar 文件中的每个 src 文件 - 但这是一项正在进行的工作,可能不适合您的需求。

jar {
    manifest {
        attributes("Implementation-Title": project.name,
                "Implementation-Version": version,
                "Main-Class": 'com.your.full.path.to.Main'
        )
    }
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
    from('src/main/resources/'){
        include('database.properties')
        into('')
    }
}

sourceSets {
    main {
        resources {
            srcDirs "src/main/resources", "src/main/configs"
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 2014-03-30
    • 2021-09-11
    • 1970-01-01
    • 2017-09-13
    • 2021-12-10
    • 1970-01-01
    相关资源
    最近更新 更多