未重新编码的资源仅在out文件夹和gradle中运行

w80xi6nr  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(292)

我在用gradle运行简单的javafx应用程序时遇到问题。
我有空白标准SAMPL.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 图书馆是一个试图修复它,是没有必要的。
my 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中解决的问题吗?

cx6n0qe3

cx6n0qe31#

只是想说清楚,我还没有找到任何真正好的答案-javafx和gradle只是不是朋友,intellij的实现版本并没有让它变得更容易。
从gradle插件创建的jar文件 application 不包括所需的javafx文件-您需要包括来自openjfx的新依赖项,以实际构建正在运行的javafxjar文件。
这是依赖项的设置,实际上并不需要:

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

相关问题