Icons disappearing in uberjar made with Leiningen
I have made a funky application in Clojure with a simple Swing gui containing some JButtons with some icons, and I want my non-programmer friends to be able to use it too.
It works all fine on my own computer when I start it with 'lein run' but when I create a package with 'lein uberjar' and run it, the icons have disappeared leaving the JButtons blank.
The icons are .png's in the 'resources' folder in the project root, and are visible in the .jar after packaging. To load the icons, I do this:
(defn get-icon [icon]
(.getFile (clojure.java.io/resource icon)))
(def some-button (JButton. (ImageIcon. (get-icon "foo.png"))))
I have tried to put in a little print statement to see, what is going on:
(println (get-icon "foo.png"))
When doing 'lein run' it prints
/home/pelle/lein/foo/resources/foo.png
and when doing 'java -jar' on the packaged .jar it prints
file:/home/pelle/lein/foo/target/znuli-0.1.2-standalone.jar!/foo.png
which is exactly where foo.png is at (except I am not entirely sure what is going on with the exclamation mark), but it is still not drawn in the Swing gui.
I have also tried explicitly specifying the resources folder in the project.clj with
:resource-paths ["resources"]
but it changes nothing.
So basically my question can be boiled down to this: How to use 'lein uberjar' to create a working hello-world.jar with a JButton containing a custom png-image?
ImageIcon
should work fine on a URL. Drop the .getFile
call and just use the result of clojure.java.io/resource
directly.