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). 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
.
My Java Code:
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.
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:
rootProject.name = 'minerva'
I also checked the resource patterns in the settings of IntelliJ, they should be valid: !?.java;!?.form;!?.class;!?.groovy;!?.scala;!?.flex;!?.kt;!?.clj;!?*.aj
Gradle has a similar project structure as maven and seperates between sources and resources.
Ressources should be put into src/main/resources
.
Move your gui.fxml
file into the resource
folder and it should be found.
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.