I recently changed the build path for a couple of packages in an eclipse project. Ultimately the file structure should have stayed the same however. So what was once the package main.java.edu.umb.cs is now edu.umb.cs. All the links were working fine before, but now I'm getting the error that Tokanagrammar.fxml can't be found. The following is in Tokanagrammar.java (the topmost expanded package in the image below):
..getResource("../../../../resources/fxml/Tokanagrammar.fxml"));
EDIT1: code to read resources in
public void start(Stage primaryStage) {
try {
AnchorPane page = (AnchorPane) FXMLLoader.load(Tokanagrammar.class.getResource("../../../../resources/fxml/Tokanagrammar.fxml"));
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.setTitle("Tokanagrammar 0.1");
primaryStage.show();
} catch (Exception ex) {
Logger.getLogger(Tokanagrammar.class.getName()).log(Level.SEVERE, null, ex);
}
}
Again, this worked fine before I took some of the src out of the build path. As far as I am aware, a resource does not have to be in the build path to be able to be accessed. I've had projects that have had an image folder, for instance, outside of the build path without any problems.
So what gives? I can also easily move the .fxml file into the package where Tokanagrammar.java resides and edit the link and it works just fine. However, per the specifications is has to be the way described.
Any help is greatly appreciated.
Rather than relying on (changing) paths relative to your Tokanagrammar
class, you should load the resource relative to the root of your classpath. This is especially true since the resource is in an entirely different package from the class (as far as path resolution goes). Try this instead:
AnchorPane page = (AnchorPane) FXMLLoader.load(
Thread.currentThread().getContextClassLoader().
getResource("fxml/Tokanagrammar.fxml"));
Note that this will work as long as all the resources in src/main/resources
are being copied to the root of your class path.