0

I have JavaFX project which I build with gradle 6.3. The directory structure is as follows:

src:
--main:
----java:
------my/package:
--------controller
--------model
--------view
--------MainApplication.java
----resources:
------my/package:
--------CSS
--------FXML
--------icons

In MainApplication.java I get the correct path, getClass().getResource("") returns path/to/project/folder/DLSimulator/build/resources/main/psopv/group9/. But if I query the resource path from another subdirectory like 'view' I get the path to the source of 'view' and not the path to the resources folder. So for example, if I call getClass().getResource("") from a class within the 'view' subdirectory, I get path/to/project/folder/DLSimulator/build/classes/java/main/psopv/group9/view/ which obviously causes getClass().getModule().getResourceAsStream("/icons/delete.png") to return null because there is no icons folder in the location where the 'view' subdirectory (or subpackage) is located.

Is this the correct behaviour, or should the returned path for the resources from within view/ be the same as within MainApplication.java? If so how can I correct this, so I get the correct path to the resource folder from whichever subdirectory I call the getResource() function?

Eagle E
  • 3
  • 4
  • So if https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other helps. – James_D May 08 '20 at 17:45
  • Don't think of resource paths as exact equivalents of file paths. In fact you must not rely on relative paths working. Instead identify the classpath roots, use the one containing the desired resource file, start there constructing the resource path and make sure it starts with `/`. – fabian May 08 '20 at 18:05
  • @fabian But also, e.g. `MainApplication.class.getResource("icons/delete.png")` would be safe as well, no? – James_D May 08 '20 at 18:15
  • @James_D Thanks for the great Q&A post. The problem was that I used relative paths instead of absolute paths and I needed to prepend the path with the package path. So replacing "icons/black/delete.png" by "/psopv/group9/icons/black/delete.png" solved the problem. – Eagle E May 08 '20 at 19:11

0 Answers0