Jerry Preissler
07/02/2023, 12:59 PMJerry Preissler
07/02/2023, 12:59 PMval resource = this.javaClass.getResource("dummy.pdf")
but I only get null. When I get the current directory for the test with .getResource(".")
I see that it is the package directory under build/classes/kotlin/test
. So if I use
val resource = this.javaClass.getResource("../../../../../../../../resources/test/dummy.pdf")
it works, but that seems impractical. Is it really supposed to work this way?James
07/02/2023, 1:06 PMJames
07/02/2023, 1:06 PMJames
07/02/2023, 1:06 PMAdam S
07/02/2023, 1:10 PMthis.javaClass.getResource("dummy.pdf")
fetches a path relative to the path of the compiled file in the assembled JAR.
The assembled JAR is the contents of both src/main/resources
and the compiled sources in src/main/kotlin/
combined together, but each Kotlin file will be placed into a directory matching the package.
A file src/main/resources/foo/bar/my/application/dummy.pdf
will be copied into the JAR with a path of /foo/bar/my/application/dummy.pdf
.
A class with an FQN of <http://foo.bar.my|foo.bar.my>.application.MyClass
will be compiled into the JAR with a path of /foo/bar/my/application/MyClass.class
So MyClass::class.javaClass.getResource("dummy.pdf")
will try and load a file relative to /foo/bar/my/application/
, and that file doesn’t exist. Instead, you can load a file with an absolute path by adding a leading /
this.javaClass.getResource("/foo/bar/my/application/dummy.pdf")
or try and add enough `../`’s to navigate to the correct path. Or put dummy.pdf
into a resources dir that matches the class package.Jerry Preissler
07/02/2023, 1:43 PMStefan Oltmann
07/12/2023, 9:25 AM