[英]Why doesn't clojure.java.io/resource work in this case?
I'm trying to load an absolute resource via clojure.java.io/resource
and it cannot locate it. 我正在尝试通过
clojure.java.io/resource
加载绝对资源,但找不到它。 However, I can retrieve it by other means. 但是,我可以通过其他方式检索它。
The example below demonstrates this. 下面的示例演示了这一点。 Create a file in the base src directory for a fresh project, and attempts to locate it via.
在基本src目录中为一个新项目创建一个文件,然后尝试通过它定位。
resource
do not succeed, but I can get to it via. resource
不会成功,但是我可以通过它获得成功。 .getResource
on the System class. System类上的
.getResource
。
How can I load this via. 我如何通过加载。
resource
? resource
? Any ideas why this is like this? 任何想法为什么会这样?
eg 例如
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=>
You need to add src to your resource-paths in project.clj: 您需要将src添加到project.clj中的资源路径中:
:resource-paths ["src"]
Those directories are included on the classpath and thus available to load resources. 这些目录包含在类路径中,因此可用于加载资源。
Also, you need to remove the leading /
: 另外,您需要删除前导
/
:
(io/resource "world.txt")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.