Using uberjar resources

This post concerns the problem of using a resource file encapsulated in the jar with the source code files.

In my case, I had a third party function that expected a BufferedReader, which should have the data of my resource file located inside the jar. The solution for my problem came from this post and the Clojure code version follows below:

(defn get-uberjar-resource
  "Finds the resource inside the jar and returns a BufferedReader object. The
  resource path is the expected path inside the jar."
  [path-str]
  (-> (Thread/currentThread)
      (.getContextClassLoader)
      (.getResourceAsStream path-str)
      (InputStreamReader.)
      (BufferedReader.)))

Note that clojure.java.io/resource returns the URL of the desired resource, which can’t be handle as a File.


Do you wanna tell me something? Please, send me an email! =)

Published: June 09 2014