In Intellij IDEA, I am trying to load resources in my Maven JavaFX modular project.
Here's my project's structure :
myProject
- src
- main
- java
- com
- example
- myPackage
- myApp.java
- module-info.java
- resources
- fonts
- font.ttf
- font.ttf
I duplicated the font I am trying to load in resources to test the following :
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL rootFile = loader.getResource("font.ttf"); // this works
URL subfolderFile = loader.getResource("fonts/font.ttf"); // this does not work
System.out.println(rootFile); // file:/C:/Users/... stuff .../myProject/target/classes/font.ttf
System.out.println(subfolderFile); // null, was expecting file:/C:/Users/... stuff .../myProject/target/classes/fonts/font.ttf
I was unable to find any posts answering my problem. Does anyone have an explanation ?
Note the documentation of
ClassLoader#getResource(String):You have a
module-info.javafile, so your code is in a named module. Thefonts/font.ttfresource is in thefontspackage, so it is encapsulated. Whereas thefont.tffresource is at the root of the module, meaning it is not in a package, thus it is not encapsulated.If you add:
To your
module-info.javafile then you should be able to loadfonts/font.ttfvia aClassLoader.That said, it is often simpler to use
Class#getResource(String). Particularly if the caller, the class, and the resource are all in the same module.