0

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.

Package Explorer

Perception
  • 79,279
  • 19
  • 185
  • 195
Matt
  • 5,408
  • 14
  • 52
  • 79
  • Edit your question please, to include the code you are using to read the resources in. – Perception Mar 24 '13 at 22:22
  • @Perception. I believe I did that correctly. I assume mean more than just the snippet I gave prior to the edit. – Matt Mar 24 '13 at 22:34
  • there are multiple classes that implement the `getResource` method, it was necessary to see which one you are using. – Perception Mar 24 '13 at 22:39

1 Answers1

1

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.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • Thanks for your response. Of course my first instinct was to copy this to see if it works -- but get the same error. Could you give me just a couple more details? What do you refer to as the _root of my class path_? Is there something else that I would have to change in the file structure noted in my post? I have pretty much always dealt with resources the same way (relative to the class that needs the resource). – Matt Mar 24 '13 at 22:50
  • 1
    Well, what you showed in your IDE is your source code structure. At some point, that structure needs to be compiled and bundled into a deployment, or runtime structure. The runtime structure defines your classpath. If you are using Maven, then all resources in `src/main/resources` get copied to your classpath the same way as all classes from `src/main/java`. The root of your classpath is the very highest level of it, conceptually the path starting with `/`, that all resources can be addressed from. – Perception Mar 24 '13 at 23:03
  • For example, from the root of the classpath, I can find `edu.umb.cs.Tokanagrammar`, because of how you defined your source folder `src/main/java`. If you consider the `src/main/resources` folder as another 'root', then your XML file can be referred to as `fxml.Tokanagrammer` (which in resource syntax is `fxml/Tokanagrammer.fxml`). – Perception Mar 24 '13 at 23:06
  • Alright. Well by what you're saying then my relative path should have worked as well -- but it does not. And if I'm not using maven, then your answer does not apply. Is this correct? – Matt Mar 25 '13 at 00:06
  • Another thing I did try with your code was to remove the "fxml/" in "fxml/Tokanagrammar.fxml" and placed Tokanagrammar.fxml in the same package as Tokanagrammar.java (edu.umb.cs) and I still get the same error, whereas with the way I call getResource originally would run if placed in the same package. – Matt Mar 25 '13 at 00:08
  • 1
    @Matt8541 - right click on your project, select New->Source Folder, then type in 'src/main/resources' and click OK. That will enable Eclipse to find your resource files when running your tests within it. – Perception Mar 25 '13 at 03:24
  • Amazing... Although not very intuitive, I will remember this. Thanks Perception! – Matt Mar 25 '13 at 06:48