[英]Clojure: How does clojure.java.io/resource load file?
Could clojure.java.io/resource load file from classpath but outside of the jar file? clojure.java.io/resource可以从classpath加载文件但在jar文件之外吗? 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. 当我把文件放在jar文件中时,它会加载文件但是当我把文件放在jar之外但是在classpath中它不会加载文件。
Example 例
Jar name: hello.jar within jar there is file hello.txt jar名称:jar中的hello.jar有文件hello.txt
java -jar hello.jar
I saw no problem to read file hello.txt file using line bellow 我看到使用line bellow读取文件hello.txt文件没有问题
(->
"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. 但是当我把hello.txt放在jar之外但是在classpath中时,它无法加载文件。
java -cp . -jar hello.jar
hello.txt file in same directory with hello.jar file. 与hello.jar文件在同一目录中的hello.txt文件。
Br, Mamun Br,Mamun
You can't mix -cp
and -jar
cmdline arguments in that way. 你不能以这种方式混合-cp
和-jar
cmdline参数。 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 ) 进入包含本地目录的jar META-INF/MANIFEST.MF
文件。( 详情 )
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. 作为目录,因为如果jar文件移动,这将容易出错或者可能出现安全问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.