【问题标题】:GetResource method is not working in java 11GetResource 方法在 java 11 中不起作用
【发布时间】:2019-05-10 04:17:43
【问题描述】:

这是我的项目结构:

这是我的模块信息:

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") 但它也不起作用。

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

【问题讨论】:

  • Gradle 和 Maven 需要将资源文件(如 FXML 文件)添加到 src/main/resources 文件夹中。然后getClass().getResource("/login.fxml") 应该可以工作,或者ClientApplication.class.getResource("/login.fxml"),或者ClientApplication.class.getResource("login.fxml"),如果你将它添加到src/main/resources/org.apj.client.app/login.fxml
  • 我已经尝试了所有可能的选项仍然为空:(
  • 您还必须将您的模块打开到javafx.fxml,将opens org.apj.client.app to javafx.fxml 添加到您的模块信息类。另外,发布您获得的堆栈跟踪,这将有助于找出问题的根源。
  • @JoséPereda 打开包到javafx.fxml 只需要允许对控制器类进行反射访问(而且我在包中看不到控制器)。并且您通过“绕过”模块系统的URL 提供资源的位置,因此资源的封装不应该成为问题。

标签: java javafx resources


【解决方案1】:

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

  1. 使用插件'org.openjfx.javafxplugin' version '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
        ]       
    }
    }
    

【讨论】:

    【解决方案2】:

    所以我通过使用java.io.File 解决了我的问题。首先,我创建了新文件并检查了它的保存位置。然后,因为我现在知道了根,所以我找到了我的资源路径并创建了一个新的文件对象 - File myFile = new File("path_to_my_resource")。然后我使用 toURI 和 toURL 方法获得了 URL - myFile.toURI().toURL()

    这绝对是 hack,但如果有人像我一样苦苦挣扎,它可以用作快速修复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 2017-04-01
      • 2015-05-28
      • 1970-01-01
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多