Ali Al-Baity
04/03/2024, 10:52 AMsourceSets {
main {
resources {
setSrcDirs(listOf("src/main/resources", "../external-resources/json-folder/"))
}
}
}
This solves my problem when I run ./gradlew build
locally, the files from that folder are all available in /build/resources/
for the module. However, when I run the jar like this:
java -jar build/libs/my-service-0.0.1.jar
I get a NullPointerException when trying to fetch one of the json files form resources.
I get the resource like this from inside the module:
javaClass.classLoader.getResource("./json-file.json")
Am I doing something wrong? Why is that file fetchable when building the jar and running unit tests that call the code to fetch that file but when running the jar I get the NullPointerException?
Any help would be appreciated, thanks!Vampire
04/03/2024, 11:09 AMsrcDir("../external-resources/json-folder/")
to add that resource dir, instead of setSrcDirs
and listing all, but that is not influencing your problem.Vampire
04/03/2024, 11:10 AMVampire
04/03/2024, 11:11 AMAli Al-Baity
04/03/2024, 11:15 AMAli Al-Baity
04/03/2024, 11:15 AMVampire
04/03/2024, 11:16 AMVampire
04/03/2024, 11:17 AMgetResource
on a class loader it is always absolute from the classpath root and must neither start with .
nor /
.
If you do getResource
on a class, it is always relative to that classes package, unless you start with /
which makes it absolute.Ali Al-Baity
04/03/2024, 11:17 AM