clojure.java.io/resource 能否从类路径加载文件但在 jar 文件之外?当我将文件放在 jar 文件中时,它会加载文件,但是当我将文件放在 jar 之外但在类路径中时,它不会加载文件。
例子
jar 名称: hello.jar jar 中有文件 hello.txt
java -jar hello.jar
Run Code Online (Sandbox Code Playgroud)
我发现使用下面的行读取文件 hello.txt 文件没有问题
(->
"hello.txt"
(clojure.java.io/resource)
(clojure.java.io/file)
(slurp))
Run Code Online (Sandbox Code Playgroud)
但是当我将 hello.txt 放在 jar 之外但在类路径中时,它无法加载文件。
java -cp . -jar hello.jar
Run Code Online (Sandbox Code Playgroud)
hello.txt 文件与 hello.jar 文件位于同一目录中。
Br, 马蒙
您不能以这种方式混合-cp
使用cmdline 参数。-jar
你可以做...
java -cp ".:hello.jar" com.foo.Class # should use ; instead of : on Windows
Run Code Online (Sandbox Code Playgroud)
或添加一个
Class-Path: /some/dir/with/hello /some/dir/with/hello/hello.jar
Run Code Online (Sandbox Code Playgroud)
包含本地目录的jarMETA-INF/MANIFEST.MF
文件的条目。(详细信息)
我建议你不要使用 . 作为目录,因为如果 jar 文件移动,这很容易出现错误或可能出现安全问题。
归档时间: |
|
查看次数: |
1297 次 |
最近记录: |