java - GetResource 方法在 java 11 中不起作用

标签 java javafx resources

这是我的项目结构: enter image description here

这是我的模块信息:

open module org.client.main {
    exports org.apj.client.app;
    requires javafx.controls;
    requires javafx.base;
    requires javafx.fxml;
    requires spring.context;
    requires spring.web;
    requires java.sql;
    requires spring.beans;
    requires spring.core;
}

这是我的父级 gradle 构建文件:

subprojects {
    afterEvaluate {
        repositories {
            jcenter()
        }

        compileJava {
            doFirst {
                options.compilerArgs = [
                        '--module-path', classpath.asPath,
                ]
                classpath = files()
            }
        }

        compileTestJava {
            inputs.property("moduleName", moduleName)
            doFirst {
                options.compilerArgs = [
                        '--module-path', classpath.asPath,
                        '--add-modules', 'junit',
                        '--add-reads', "$moduleName=junit",
                        '--patch-module', "$moduleName=" + files(sourceSets.test.java.srcDirs).asPath,
                ]
                classpath = files()
            }
        }

        test {
            inputs.property("moduleName", moduleName)
            doFirst {
                jvmArgs = [
                        '--module-path', classpath.asPath,
                        '--add-modules', 'ALL-MODULE-PATH',
                        '--add-reads', "$moduleName=junit",
                        '--patch-module', "$moduleName=" + files(sourceSets.test.java.outputDir).asPath,
                ]
                classpath = files()
            }
        }
    }
}

这是我的客户端模块构建文件:

plugins {
    id 'java'
}

group 'APJ'
version '1.0-SNAPSHOT'

sourceCompatibility = 11

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.springframework', name: 'spring-webmvc', version: '5.1.4.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

ext.moduleName = 'org.client.main'

我正在尝试获取 FXMLLoader 的资源,但我根本无法让它工作。我已经挣扎了2个小时了,现在我真的很绝望。我已经尝试了文件名的所有可能的组合、每个可能的位置,但它仍然返回 null。

我也尝试过ClassLoader.getSystemClassLoader().getResource("/login.fxml") getClass().getResource("/login.fxml") 但它也不起作用。

有人可以帮我解决这个问题吗?我将非常感激。

最佳答案

我注意到 Java 11 有 2 个选项可以让 getResource 工作:

  1. 使用插件'org.openjfx.javafxplugin'版本'0.0.7'
  2. 在上面 Slaw 提供的链接中使用“--patch-module”:Where do resource files go in a Gradle project that builds a Java 9 module?

    run {
    doFirst {
        jvmArgs = [
                '--module-path', classpath.asPath,
                '--patch-module', "$moduleName=" + files(sourceSets.main.output.resourcesDir).asPath,
                '--module', mainClassName
        ]       
    }
    }
    

关于java - GetResource 方法在 java 11 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54317046/

相关文章:

JavaFX 和 Swing 颜色不匹配

java - 马文 : how to filter the same resource multiple times with different property values?

javascript - Spring 找不到资源文件(css、jsp...)

java - 我尝试在网页上查看小程序时收到 "class not found"错误

java - 在 O(1) 中与 getKey(B) 一对一映射数据结构 (A,B)?

java - 为什么我的 Java 程序不能读取 Perl 的 STDERR?

java - 是否可以在不同(插件)运行之间保存工作空间?

java - 如何获取 Java BlockingQueue 的空属性,以便将其绑定(bind)到 JavaFX 元素的某些属性?

java - 重叠形状 - 错误的重叠形状行为

java - 用java加载图像占用了我太多的资源..有什么方法可以让我的程序看起来很漂亮?