I have this code:
val url: URL = getClass.getResource(\"com/mysite/main/test.fxml\")
and it always returns null
(or Unit
You have three options:
take advantage of relative path to your current package (where Test.class
is):
getClass.getResource("test.fxml")
you can use absolute path:
getClass.getResource("/com/mysite/main/test.fxml")
or load through the ClassLoader
(note that it always start from root):
getClass.getClassLoader.getResource("com/mysite/main/test.fxml")
In IntelliJ IDEA, make sure you have added ;?*.fxml
to the:
Settings
(Preferences
on Mac) | Compiler
| Resource Patterns.
Late answer but I just had this same problem. The root cause was an incorrect rootProject.name entry in my settings.gradle. Once I fixed that, cleaned, and rebuilt my resource were able to load using getClass().getResource(). Hopefully that helps somebody.
If it does not work, you can try with ClassLoader:
ClassLoader.getSystemResource("filename").getPath
The filename should be in the same directory layer
Possibly it's not being copied to the bin/ directory from the src/ directory? This happens on recompilation, but if you drop it into the src/ directory after the program is already compiled, the IDE won't know.