I have a gradle kotlin project , where I want to load a file from my resources directory. My kotlin classes are in src/main/kotlin/packagenames and my resources are in src/main/resources
After gradle build including kotlin plugin (kotlin("jvm") version "1.7.20") my classes are copied to build/classes/kotlin/main/packagenames and my resources to build/resources/main
Now I try to read this files from my kotlin code
object {}.javaClass.getResourceAsStream("/resources/filename")
or
object {}.javaClass.classloader.getResourceAsStream("/resources/filename")
but the files are not found.
When I try to see which directory it looks into
println(this.javaClass.getResource("/"))
results in build/classes/kotlin and with javaClass.classLoader it returns null accessing '/'
This means normally I would have to go up two directories and then access resources directory, but this doesn't work as build/classes/kotlin seems to be the root. Any Idea why this happens.
CodePudding user response:
Remove /resources/
from the input string and instead try
object {}.javaClass.getResourceAsStream("/filename")
This should work as expected.