I have this code:
val url: URL = getClass.getResource("com/mysite/main/test.fxml")
and it always returns
Unit). I have only two files in the project:
and when I run the
url value is always null.
I just tried rebuild the project, I am using IntelliJ IDEA. What am I doing wrong here?
You have three options:
take advantage of relative path to your current package (where
you can use absolute path:
or load through the
ClassLoader (note that it always start from root):
In IntelliJ IDEA, make sure you have added
;?*.fxml to the:
Preferences on Mac) |
Compiler | Resource Patterns.
Correct answer by Tomasz Nurkiewicz on January 3, 2021
If it does not work, you can try with ClassLoader:
The filename should be in the same directory layer
Answered by Newt on January 3, 2021
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.
Answered by cory.todd on January 3, 2021
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.
Answered by ConorR on January 3, 2021
1 Asked on February 22, 2021 by 1stnox
2 Asked on February 22, 2021 by liara
2 Asked on February 22, 2021 by rnj
2 Asked on February 21, 2021 by jayk23
1 Asked on February 21, 2021 by software-is-fun
0 Asked on February 21, 2021 by tester1
1 Asked on February 21, 2021 by sameer
1 Asked on February 21, 2021 by micheal-j-roberts
0 Asked on February 21, 2021 by haddadi-abdraouf
2 Asked on February 21, 2021 by bob-rathbone
2 Asked on February 20, 2021 by karl-drews
2 Asked on February 20, 2021 by m-ismail
0 Asked on February 20, 2021 by leonardokunkel
Get help from others!