Reputation: 10825
I have a standard (Non JavaFX) Maven Java Project in Netbeans. The first thing it is supposed to do is load a .fxml file, but I can't seem to find it using getClass().getResource()
.
This is unusual, because this project is imported from a standard Java project, which runs just fine without error.
Here is the main method:
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Main_PanelController controller = new Main_PanelController(this);
FXMLLoader loader = new FXMLLoader(getClass().getResource("Main_Panel.fxml"));
loader.setController(controller);
URL url = getClass().getResource("Main_Panel.fxml");
System.out.println("URL: " + url);
System.exit(0);
Parent root = loader.load();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.getIcons().add(new Image(new FileInputStream(Paths.get("Pictures", "SBU Emblem.png").toFile())));
stage.setAlwaysOnTop(true);
stage.setTitle("Simple Knisley Citation Tool α 0.5");
stage.setResizable(false);
stage.show();
}
The URL always comes back as null
.
& The File Hierarchy:
I have the System.out.println
as a test method to see if the URL object comes back as null or not. According to everything I've read about getClass().getResource()
it should get any object in the same package as the calling class, which Main_Panel.fxml
is.
Here is the Jar file structure from the target
folder after Clean & Build
has run using the command jar tf Simple-Knisley-0.5-Alpha-jackofall.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/protonmail/
com/protonmail/sarahszabo/
com/protonmail/sarahszabo/simpleknisley/
com/protonmail/sarahszabo/simpleknisley/core/
com/protonmail/sarahszabo/simpleknisley/core/CitationDiskManager.class
com/protonmail/sarahszabo/simpleknisley/core/Main_PanelController.class
com/protonmail/sarahszabo/simpleknisley/core/CitationGenerator$GeneratorType.class
com/protonmail/sarahszabo/simpleknisley/core/CitationGenerator.class
com/protonmail/sarahszabo/simpleknisley/core/Main.class
.netbeans_automatic_build
META-INF/maven/
META-INF/maven/com.protonmail.sarahszabo.simpleknisley/
META-INF/maven/com.protonmail.sarahszabo.simpleknisley/Simple-Knisley/
META-INF/maven/com.protonmail.sarahszabo.simpleknisley/Simple-Knisley/pom.xml
META-INF/maven/com.protonmail.sarahszabo.simpleknisley/Simple-Knisley/pom.properties
I'm using Java 8_171 on Netbeans 8.2 on Kubuntu 18.04 Bionic Beaver
Upvotes: 2
Views: 2053
Reputation: 7459
There are a few issues going on here.
Maven expects (by default) resources to be in the src/main/resources
directory. It will only copy java files from src/main/java
by default. So move your fxml
file to src/main/resources/com/protonmail/sarahszabo/simpleknisley/core/Main_Panel.fxml
getClass().getResource()
expects you to give the full path to the resource that you want to load. So in your case: getClass().getResource("/com/protonmail/sarahszabo/simpleknisley/core/Main_Panel.fxml")
Using getClass().getClassLoader().getResource()
allows you to specify the location of the resource you want to load as a relative path to the class you are calling it in:
getClass().getClassLoader().getResource("Main_Panel.fxml")
Upvotes: 3