4

我正在使用具有以下目录结构的 Leiningen 开发一个小型 Clojure 项目:

  • 项目
    • 源代码
    • 测试
      • xxx
        • 登录.clj
    • 资源
      • 上市
        • css
          • 引导程序.css

在 test 目录中的 login.clj 文件中,我试图在 respuces/publi/css 目录中搜索 bootstrap.css 文件:

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

返回文件未找到错误:

线程“主”java.io.FileNotFoundException 中的异常:/css/bootstrap.css(没有这样的文件或目录),正在编译:(login.clj:10)

所以问题很简单,我应该怎么做才能访问文件?

4

3 回答 3

3

您实际上需要使用“资源”-要么

  1. (clojure.java.io/resource "relative/path"),它返回文件的 URL,

  2. (noir.io/get-resource"relative/path")返回文件,或者

  3. (noir.io/slurp-resource "relative/path")返回文件的内容。

别担心——你不是第一个遇到这个问题的人!

于 2014-07-24T08:41:27.343 回答
2

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.

于 2012-07-02T04:42:45.540 回答
1

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.

于 2012-07-02T03:52:50.690 回答