Icons disappear in uberjar made with Leiningen

I 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 use it.

It works fine on my own computer when I run it with "lein run", but when I create a package with "lein uberjar" and run it, the icons disappeared, leaving the JButtons empty.

The .png icons are located in the "resources" folder at the root of the project and are visible in the .jar after packaging. To download 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 tried to introduce a small print statement to see what happens:

 (println (get-icon "foo.png")) 

When executing lein run, it prints

 /home/pelle/lein/foo/resources/foo.png 

and when you do "java -jar" on a packaged .jar, it prints

 file:/home/pelle/lein/foo/target/znuli-0.1.2-standalone.jar!/foo.png 

where exactly is foo.png (except that I'm not quite sure what is happening with the exclamation mark), but it is not yet drawn in the Swing gui.

I also tried to explicitly specify the resource folder in project.clj using

 :resource-paths ["resources"] 

but nothing changes.

So, basically my question can be summarized as follows: How to use "lein uberjar" to create a working hello-world.jar with JButton containing a custom png image?

+4
source share
1 answer

ImageIcon should work well with the url. Drop the .getFile call and just use the result of clojure.java.io/resource directly.

+2
source

All Articles