r0hitsharma
r0hitsharma

Reputation: 1762

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?

Upvotes: 4

Views: 1105

Answers (3)

Simon Brooke
Simon Brooke

Reputation: 396

You actually need to use 'resources' - either

  1. (clojure.java.io/resource "relative/path"), which returns a URL to the file,

  2. (noir.io/get-resource"relative/path") which returns the file, or

  3. (noir.io/slurp-resource "relative/path") which returns the content of the file.

And don't worry - you're not the first person to get caught with this problem!

Upvotes: 3

mblinn
mblinn

Reputation: 3274

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.

Upvotes: 2

deterb
deterb

Reputation: 4014

Check what your actual classpath is when running. Chances are the classspath references the ./resources directory, and not the ./resources/public directory.

Try using "/public/css/bootstrap.css" instead to solve this.

Upvotes: 1

Related Questions