5

My project is running properly in eclipse but when I am creating a jar file of this project and trying to run it through cmd it is showing "Location is not set" error.

My project structure is:

This is the structure of my project

The Method is (Running in eclipse):

@FXML
private void RegularCustomer(ActionEvent event) throws Exception{
    Stage stage = (Stage) dailySales.getScene().getWindow();
    Scene scene = dailySales.getScene();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("../customer/CustomerHome.fxml"));
    System.out.println(loader.getLocation());
    scene.setRoot(loader.load());
    stage.setScene(scene);
    stage.show();
}

What is wrong with this code?

There are some relative questions but they are different from it. Their code didn't run in IDE but my code run in IDE.

FYI: I made some changes in folder structure and was able to run successfully. But that structure was horrible because I put all my FXML files and controllers in same package.

1
  • 4
    @jewelsea While acknowledging there are dozens of questions like this on SO/javafx, (and I can never find them when searching) none of the answers to that one actually address the exact issue here (which I'll paraphrase as "Why does this work with a file system class loader but not with the jar class loader", which I think is really closer to stackoverflow.com/questions/13046150). Someone should probably write a catch-all "how to load a resource in JavaFX" Q/A as it seems to cause so many issues...
    – James_D
    Jan 13, 2016 at 19:46

2 Answers 2

15

When you use getClass().getResource(...) you are loading a resource, not specifying a path to a file. In the case where the class loader loads classes from the file system, these essentially equate to the same thing, and it does actually work (though even then there's no technical reason it has to). When the class loader is loading classes by other mechanisms (and probably in all cases anyway), then it's important to pay attention to the Java specifications for a resource.

In particular, note:

Resources, names, and contexts

A resource is identified by a string consisting of a sequence of substrings, delimited by slashes (/), followed by a resource name. Each substring must be a valid Java identifier. The resource name is of the form shortName or shortName.extension. Both shortName and extension must be Java identifiers.

(My emphasis.) Since .. is not a valid Java identifier, there's no guarantee of this resource being resolvable. It happens that the file system class loader resolves this in the way you expect, which is why it works in your IDE, but the implementation of getResource(...) in the jar class loader does not implement this in the way you are hoping.

Try

FXMLLoader loader = new FXMLLoader(getClass().getResource("/sm/customer/CustomerHome.fxml"));

Using controller locations to load FXML:

Since you have organized your code so that each FXML is in the same package as its corresponding controller file (which I think is a sensible way to do things), you could also leverage this in loading the FXML: just load the FXML "relative to its controller":

FXMLLoader loader = new FXMLLoader(CustomerHomeCtrl.class.getResource("CustomerHome.fxml"));

This seems fairly natural in this setup, and the compiler will check that you have the package name for CustomerHomeCtrl correct at the point where you import the class. It also makes it easy to refactor: for example suppose you wanted to split sm.admin into multiple subpackages. In Eclipse you would create the subpackages, drag and drop the FXML and controllers to the appropriate subpackages, and the import statements would automatically be updated: there would be no further changes needed. In the case where the path is specified in the getResource(...), all those would have to be changed by hand.

5
  • Why should it make little sense? A relative path is relative to the package of the class.
    – Puce
    Jan 13, 2016 at 12:15
  • Thank your for this awesome solution. It is now working perfectly.
    – Shihab
    Jan 13, 2016 at 17:53
  • @Puce Updated answer to be a bit more precise. Would be interested if that makes more sense?
    – James_D
    Jan 13, 2016 at 18:27
  • Well IMHO the real problem is the filesystem you operate on. Native Filesystems support ".." whereas jar-file-systems don't
    – tomsontom
    Feb 20, 2017 at 14:07
  • @tomsontom I guess my point is that there is a specification for how resource naming behaves that is independent of the file system. As long as your resource name conforms to that specification, it will work regardless of how the application is deployed. (In theory, even if the underlying file system had more stringent requirements than the resource naming spec.)
    – James_D
    Feb 20, 2017 at 14:13
1

A bit late but this can maybe help someone. If you are using IntelliJ, your resources folder may not be marked as THE resources folder, which has the following icon: enter image description here

This is the way I fixed it: enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.