简体   繁体   English

GetResource 方法在 Java 11 中不起作用

[英]GetResource method is not working in java 11

This is my project structure:这是我的项目结构: 在此处输入图片说明

This is my module-info:这是我的模块信息:

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

This is my parent gradle build file:这是我的父 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()
            }
        }
    }
}

This is my client module build file:这是我的客户端模块构建文件:

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'

I am trying to getResource for FXMLLoader but I simply can not get it to work.我正在尝试为 FXMLLoader 获取资源,但我根本无法让它工作。 I have been struggling for 2 hours and I am really desperate right now.我已经挣扎了2个小时,现在我真的很绝望。 I have tried like every possible combination of file name, every possible location but still it is returning null.我尝试了所有可能的文件名组合,每个可能的位置,但它仍然返回空值。

Also I have tried ClassLoader.getSystemClassLoader().getResource("/login.fxml") getClass().getResource("/login.fxml") But it is also not working.我也试过ClassLoader.getSystemClassLoader().getResource("/login.fxml") getClass().getResource("/login.fxml")但它也不起作用。

Can someone help me with this ?有人可以帮我弄这个吗 ? I would be very grateful.我会很感激。

I noticed with Java 11, 2 options to make getResource work:我注意到在 Java 11 中,有 2 个选项可以使 getResource 工作:

  1. use plugin 'org.openjfx.javafxplugin' version '0.0.7' or使用插件'org.openjfx.javafxplugin' version '0.0.7'
  2. use '--patch-module' in the link provided by Slaw above: Where do resource files go in a Gradle project that builds a Java 9 module?在上面 Slaw 提供的链接中使用“--patch-module”: 资源文件在构建 Java 9 模块的 Gradle 项目中的位置?

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

So I have solved my issue by using java.io.File .所以我通过使用java.io.File解决了我的问题。 Firstly I have created new File and checked where it is being saved.首先,我创建了新文件并检查了它的保存位置。 Then, since I have now known the root I have found my path to my resource and created a new File object - File myFile = new File("path_to_my_resource") .然后,因为我现在知道了根,所以我找到了我的资源路径并创建了一个新的 File 对象 - File myFile = new File("path_to_my_resource") Then I got URL using toURI and toURL methods - myFile.toURI().toURL() .然后我使用 toURI 和 toURL 方法获得了 URL - myFile.toURI().toURL()

This is definitely hack but if someone is struggling like I was it can be used as a quick fix.这绝对是 hack 但如果有人像我一样挣扎,它可以用作快速修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM