简体   繁体   English

Clojure:clojure.java.io/resource如何加载文件?

[英]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.

相关问题 clojure.java.io ClassNotFoundException - clojure.java.io ClassNotFoundException 为什么在这种情况下clojure.java.io/resource不起作用? - Why doesn't clojure.java.io/resource work in this case? 我如何使用 lein exec 要求 clojure.java.io? - How do I require clojure.java.io with lein exec? 没有方法的实现::协议的as-file:#'clojure.java.io /为类发现的强制:clojure.lang.PersistentVector - No implementation of method: :as-file of protocol: #'clojure.java.io/Coercions found for class: clojure.lang.PersistentVector 要编写下载器,请使用Clojure.java.io还是Java的io api? - To write a downloader, Clojure.java.io or Java's io api? clojure的读取文件结构,即with-open和clojure.java.io/reader,是否足够频繁访问? - Is clojure's read file structure, i.e. with-open and clojure.java.io/reader, efficient enough for frequent access? 没有实现方法:: make-reader of protocol:#'clojure.java.io / IOFactory for class:nil - No implementation of method: :make-reader of protocol: #'clojure.java.io/IOFactory found for class: nil Clojure Clostache错误-没有实现方法::make-reader of protocol:#'clojure.java.io / IOFactory为类nil找到 - Clojure Clostache error - No implementation of method: :make-reader of protocol: #'clojure.java.io/IOFactory found for class: nil 如何在leiningen repl中预加载clojure文件? - How does one pre-load a clojure file in the leiningen repl? 如何在 Clojure 中动态创建文件资源? - How to create a file resource on the fly in Clojure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM