Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

clojure

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

like image 662
Mamun Avatar asked Oct 21 '22 13:10

Mamun


1 Answers

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.

like image 104
sw1nn Avatar answered Nov 03 '22 00:11

sw1nn