Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources in Clojure applications

I am using Leiningen in my Clojure project (a GUI application) and created a "resources" directory under the project root to hold images that my app uses.

When I am running my app locally during testing, I fetch the images using the relative path "resources/logo.png", and this works fine. But when I build an uberjar using Leiningen, Leiningen puts the files from the resources folder in the JAR's root folder, so my references to resource files don't work anymore.

What is the correct way to access resources like this using Leiningen?

like image 842
Kevin Albrecht Avatar asked Nov 04 '11 13:11

Kevin Albrecht


People also ask

Does clojure need JVM?

Clojure Overview. Clojure is a dynamically-typed, functional programming language that runs on the JVM (Java 5 or greater) and provides interoperability with Java. A major goal of the language is to make it easier to implement applications that access data from multiple threads (concurrency).

Is clojure still used?

Clojure (still) for Start-ups While more large companies are adopting Clojure than ever, the sweet spot is still the smaller companies of less than 100 employees. The reasons that start-ups choose Clojure are many and variegated: Leverage - small effort, big result.

What is clojure used for?

Clojure is designed to be a hosted language, sharing the JVM type system, GC, threads etc. All functions are compiled to JVM bytecode. Clojure is a great Java library consumer, offering the dot-target-member notation for calls to Java. Clojure supports the dynamic implementation of Java interfaces and classes.

What is Clojure REPL?

A Clojure REPL (standing for Read-Eval-Print Loop) is a programming environment which enables the programmer to interact with a running Clojure program and modify it, by evaluating one code expression at a time.


2 Answers

The previous answerer (skuro) pointed out that I need to get the file from the classpath. After a little more digging, this appears to be the solution that works for my case:

(.getFile (clojure.java.io/resource "foo.png")) 
like image 151
Kevin Albrecht Avatar answered Oct 06 '22 05:10

Kevin Albrecht


Just a syntax sugar for the answer of Kevin Albrecht:

(require '[clojure.java.io :as io])  (-> "foo.png" io/resource io/file)  
like image 33
Valerii Hiora Avatar answered Oct 06 '22 04:10

Valerii Hiora