简体   繁体   中英

Clojure: How does clojure.java.io/resource load file?

Could clojure.java.io/resource load file from classpath but outside of the jar file? When I put file within jar file, it'd load file but when I put file outside jar but in classpath it'd not load file.

Example

Jar name: hello.jar within jar there is file hello.txt

java -jar hello.jar 

I saw no problem to read file hello.txt file using line bellow

(->
  "hello.txt"
  (clojure.java.io/resource)
  (clojure.java.io/file)
  (slurp))

But when I put hello.txt outside of the jar but in classpath, it fails to load file.

java -cp . -jar hello.jar 

hello.txt file in same directory with hello.jar file.

Br, Mamun

You can't mix -cp and -jar cmdline arguments in that way. You can either do...

java -cp ".:hello.jar" com.foo.Class  # should use ; instead of : on Windows

or add a

Class-Path: /some/dir/with/hello /some/dir/with/hello/hello.jar

entry to the jar META-INF/MANIFEST.MF file that includes local directory.( details )

I would recommend you don't use . as the directory, since this will be prone to errors or maybe security issues if the jar file moves.

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