<p>Context: I successfully parse a json file while debugging/running locally in my Windows PC that way</p>
<pre><code>import com.fasterxml.jackson.core.JsonFactory
import com.fasterxml.jackson.core.JsonParser
import java.io.File
import java.nio.file.Paths

...

val basePath = Paths.get(&quot;&quot;).toAbsolutePath().toString()

val parser: JsonParser = JsonFactory().createParser(File(&quot;$basePath/app/src/main/resources/support/certain-file.json&quot;))
</code></pre>
<p>When I deployed it to either docker or AWS I get this error</p>
<pre><code> /app/src/main/resources/support/certain-file.json (No such file or directory)
</code></pre>
<p>Is there a more reliable way to &quot;navigate&quot; to a file placed in resources folder? I mean someway it wouldn't be affected either it is Windows/Linux/Kubernetes/etc?</p>

Jim C
<p>The usual way to access resource files is via the classpath, not by going directly to the filesystem.</p>
<p>There are several advantages to this:</p>
<ul>
<li>The resources will usually be deployed automatically, along with your compiled code; no need for manual copying.  (Maven and Gradle usually know to treat the <code>src/main/resources</code> folder this way.)</li>
<li>If you build the application as a .jar/.war file, the resources will get included in that.  (Your code doesn't need to know or care that they're in an archive; as long as you access the resource as a stream, loading just works.)</li>
<li>The application isn't tied to a particular directory layout or OS; it can run in any directory on any machine.</li>
<li>You can override files.  Because the classpath can give several locations, a file in a later location can be overridden by adding a file into an earlier location.</li>
</ul>
<p>It's easy enough to do: just call <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)" rel="nofollow noreferrer"><code>getResourceAsStream()</code></a> on the relevant classloader.  In this case, it could be as simple as:</p>
<pre><code>val parser = javaClass.classLoader.getResourceAsStream(&quot;support/certain-file.json&quot;)?.use {
    JsonFactory().createParser(it)
}
</code></pre>
<p>Here <code>javaClass.classLoader</code> gets the current class's <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html" rel="nofollow noreferrer">ClassLoader</a>.  (You could use the <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getSystemClassLoader--" rel="nofollow noreferrer">system</a> one instead, of course.)</p>
<p><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html" rel="nofollow noreferrer"><code>use()</code></a> takes care of closing the resulting <a href="https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html" rel="nofollow noreferrer"><code>InputStream</code></a> once it's been read.</p>
<p>And luckily, there's a <a href="https://fasterxml.github.io/jackson-core/javadoc/2.10/com/fasterxml/jackson/core/JsonFactory.html#createParser-java.io.InputStream-" rel="nofollow noreferrer"><code>createParser()</code></a> method which accepts the <code>InputStream</code>.</p>
<p>(Disclaimer: I don't know anything about Kubernetes, or about running Kotlin/Java programs on Windows.  But the beauty of using the classloader is that you don't need to!  If it can load classes, it can load resources too.)</p>
