[英]Why doesn't clojure.java.io/resource work in this case?
我正在尝试通过clojure.java.io/resource
加载绝对资源,但找不到它。 但是,我可以通过其他方式检索它。
下面的示例演示了这一点。 在基本src目录中为一个新项目创建一个文件,然后尝试通过它定位。 resource
不会成功,但是我可以通过它获得成功。 System类上的.getResource
。
我如何通过加载。 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=>
您需要将src添加到project.clj中的资源路径中:
:resource-paths ["src"]
这些目录包含在类路径中,因此可用于加载资源。
另外,您需要删除前导/
:
(io/resource "world.txt")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.