[英]Can't load resource, always returns null
I have the following problem: 我有以下问题:
I am developing a Java 8 Application with IntelliJ IDEA 15 using JavaFX as GUI and Gradle as build automation system (never worked with Gradle before though). 我正在使用IntelliJ IDEA 15开发Java 8应用程序,使用JavaFX作为GUI和Gradle作为构建自动化系统(尽管之前从未与Gradle一起使用)。 I always get an IllegalStateException
"Location is not set" when loading the fxml file, the reason is because getClass().getResource("gui.fxml")
always returns null
. 加载fxml文件时,总是收到IllegalStateException
“未设置位置”,原因是因为getClass().getResource("gui.fxml")
始终返回null
。
My Java Code: 我的Java代码:
package minerva.gui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
System.out.println(getClass().getResource("gui.fxml")==null); // DEBUG: always returns true, but why?
FXMLLoader loader = new FXMLLoader(getClass().getResource("gui.fxml"));
Parent root = (Parent) loader.load();
primaryStage.setTitle("Minerva");
primaryStage.setScene(new Scene(root));
Controller control = loader.getController();
primaryStage.show();
}
}
Maybe Gradle is responsible for this, I don't know. 我不知道Gradle对此负责。
build.gradle: build.gradle:
group 'minerva.impl'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
compile 'org.hsqldb:hsqldb:2.3.1'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
settings.gradle: settings.gradle:
rootProject.name = 'minerva'
I also checked the resource patterns in the settings of IntelliJ, they should be valid: !? 我还检查了IntelliJ设置中的资源模式,它们应该有效:!? .java;!? .java;!? .form;!? 。形成;!? .class;!? 。类;!? .groovy;!? .groovy;!? .scala;!? .scala;!? .flex;!? 。柔性;!? .kt;!? .kt;!? .clj;!?*.aj .clj;!?*。aj
Gradle has a similar project structure as maven and seperates between sources and resources. Gradle具有与Maven类似的项目结构,并且在源和资源之间进行分隔。 Ressources should be put into src/main/resources
. src/main/resources
应该放在src/main/resources
。
Move your gui.fxml
file into the resource
folder and it should be found. 将gui.fxml
文件移动到resource
文件夹中,应该可以找到它。
Your resulting artifacts are placed in $PROJECT/target
and if you have similar problems in the future, it is a good idea to look into your jar,war,ear
or what ever archive in order to see, if the files are packed up as expected. 您将生成的工件放置在$PROJECT/target
,如果以后遇到类似的问题,则最好查看jar,war,ear
或任何存档,以查看文件是否打包为预期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.