java - GetResource 方法在 java 11 中不起作用
问题描述
这是我的模块信息:
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 工作:
- 使用插件
'org.openjfx.javafxplugin' version '0.0.7'
或 在上面 Slaw 提供的链接中使用“--patch-module”:资源文件在构建 Java 9 模块的 Gradle 项目中位于何处?
run { doFirst { jvmArgs = [ '--module-path', classpath.asPath, '--patch-module', "$moduleName=" + files(sourceSets.main.output.resourcesDir).asPath, '--module', mainClassName ] } }
推荐阅读
- c# - 在linkedin分享一张图片时,哎呀!错误显示
- ios - 我的 UE4 演示应用程序在启动后崩溃
- json - 角度启动错误 npm ERR!代码生命周期
- javascript - javascript函数传递具有空值的对象
- python-3.x - 如何修复:cx_Oracle.DatabaseError:DPI-1047:找不到 64 位 Oracle 客户端库 - Python
- javascript - 如何在javascript中将数据从旧json复制到新json?
- php - shell_exec 可以运行我的机器学习代码吗?它在终端上运行良好,但在 shell_exec 中返回 NULL
- c# - NET Core 跨 Docker 服务副本同步变量值
- openvpn - Mikrotik 多站点 OpenVPN。客户不可见
- javascript - AWS:尝试加载预签名 URL 时拒绝访问(直接文件上传浏览器)