gpt4 book ai didi

java - 模块化 JavaFX 导致子目录类加载失败

转载 作者:行者123 更新时间:2023-12-03 03:55:19 29 4
gpt4 key购买 nike

我制作了一个 Gradle 项目,它使用类加载器从子目录资源/文本中加载文本文件。此时它可以工作,但是当我将项目转换为模块化 JavaFX 程序时,相同的类加载器函数会给出 NullPointerException。

src/主项目目录

└───src
└───main
├───java
│ │ module-info.java
│ └───app
│ Main.java
└───resources
└───text
words.txt

构建.gradle
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}

sourceCompatibility = 11

repositories {
jcenter()
}

javafx {
version = '13.0.1'
modules = [ 'javafx.controls']
}

mainClassName = 'exMod/app.Main'
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}

模块信息.java
module exMod {
requires javafx.controls;
exports app;
}

主.java
package app;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main extends Application {

public static void main(String[] args){ launch(); }

public void start(Stage stage){
stage.setTitle("Hello World!");
Button button = new Button();
Label label = new Label();
button.setText("Click me!");
button.setOnAction(event -> label.setText(getWords()));

VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(button, label);
stage.setScene(new Scene(root, 300, 250));
stage.show();
}

private String getWords(){
String path = "text/words.txt";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path);
InputStreamReader isReader = new InputStreamReader(inputStream); // null exception
BufferedReader reader = new BufferedReader(isReader);
StringBuilder content = new StringBuilder();
try {
String line;
while( (line = reader.readLine()) != null) {
content.append(line);
}
} catch(IOException e){
e.printStackTrace();
}
return content.toString();
}
}

如果我将路径变量更改为“words.txt”并将文本文件上移一级到 src/main/resources,它可以工作,但由于某种原因我无法发现,从资源子目录加载文件不管用。是什么导致 NullPointerException,我该如何解决?

最佳答案

在对 this question 的评论中, 作为 program.class.getClassLoader().getResource("b/text.txt") 的替代用户 Holger 说

... you should use program.class.getResource("b/text.txt"), to resolve a resource relative to the location of the program class. Otherwise, it may fail in Java 9 or newer, once you use modules, as going up to the class loader will destroy the information about the actual module.



因此,我尝试仅使用 getClass().getResourceAsStream(path)而不是 getClass().getClassLoader().getResourceAsStream(path) .

我还阅读了 Alan Bateman 对 this question 的评论他在哪里说

The resource is encapsulated so it can't be found with the ClassLoader APIs. I assume ComboBoxStyling.class..getResource("/css/styleclass.css") will work - note the leading slash so that it is found relative to the root location of the module, not relative to ComboBoxStyling.class in this case.



所以我碰巧一起尝试了这个组合,它奏效了!

获取单词()
private String getWords(){
String path = "/text/words.txt";
InputStream in = getClass().getResourceAsStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder content = new StringBuilder();
try {
String line;
while( (line = reader.readLine()) != null) {
content.append(line);
}
} catch(IOException e){
e.printStackTrace();
}
return content.toString();
}

关于java - 模块化 JavaFX 导致子目录类加载失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60859856/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com