简体   繁体   中英

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. 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?

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:

: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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM