Search code examples
clojureleiningennoir

Acessing resource files in clojure webnoir project


I am working on a small Clojure project using Leiningen with the following directory structure:

  • project
    • src
    • test
      • xxx
        • login.clj
    • resources
      • public
        • css
          • bootstrap.css

In the login.clj file in the test directory, I'm trying to slurp the bootstrap.css file in the respuces/publi/css directory:

(def css-file "/css/bootstrap.css")
(def css-contents (slurp css-file))

Which returns the file not found error:

Exception in thread "main" java.io.FileNotFoundException: /css/bootstrap.css (No such file or directory), compiling:(login.clj:10)

So the question is simple, what should I do in order to access the file?


Solution

  • Slurp will read a file off the filesystem, not the classpath. So what with what you're trying to do there, you're attempting to read a file at /css/bootstrap.css from the root of your filesystem, and it doesn't exist, just like the error message says.

    If you're just reading it in for testing purposes, then you should be able to slurp it with a relative path like resources/public/css/bootstrap.css, assuming you're running the tests off the project directory.