为什么clojure.java.io/resource在这种情况下不起作用?

Bil*_*ill 1 clojure

我正在尝试加载绝对资源clojure.java.io/resource,它无法找到它.但是,我可以通过其他方式检索它.

以下示例演示了这一点.在基本src目录中为新项目创建一个文件,并尝试通过它找到它.resource没有成功,但我可以通过它..getResource在System类上.

我怎样才能加载这个.resource?任何想法为什么会这样?

例如

C:\Users\username\temp>lein new foo
Generating a project called foo based on the 'default' template.
To see other templates (app, lein plugin, etc), try `lein help new`.

C:\Users\username\temp>cd foo   
C:\Users\username\temp\foo>cd src    
C:\Users\username\temp\foo\src>echo hello > world.txt   
C:\Users\username\temp\foo\src>cd ..    
C:\Users\username\temp\foo>lein repl

...    

user=> (require ['clojure.java.io :as 'io])
nil
user=> (io/resource "/world.txt")
nil
user=> (.getResource System "/world.txt")
#<URL file:/C:/Users/username/temp/foo/src/world.txt>
user=>
Run Code Online (Sandbox Code Playgroud)

Ale*_*ler 5

您需要将src添加到project.clj中的资源路径:

:resource-paths ["src"]
Run Code Online (Sandbox Code Playgroud)

这些目录包含在类路径中,因此可用于加载资源.

此外,您需要删除前导/:

(io/resource "world.txt")
Run Code Online (Sandbox Code Playgroud)