Reputation: 12883
I'm trying to load an absolute resource via clojure.java.io/resource
and it cannot locate it. 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. resource
do not succeed, but I can get to it via. .getResource
on the System class.
How can I load this via. resource
? Any ideas why this is like this?
e.g.
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=>
Upvotes: 1
Views: 1544
Reputation: 70201
You need to add src to your resource-paths in 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")
Upvotes: 5