java 从 jar 或类路径上的任何位置读取文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2467362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:18:52  来源:igfitidea点击:

Reading a file from a jar, or anywhere on the classpath?

javamaven-2file-iojar

提问by Stefan Kendall

I'm trying to build an application that builds a resource file into a jar, but I'd like to have the project runnable within eclipse. I have a basic maven 2 structure for my project, and I'm unsure how to read in the file such that it's found and used when run from the JAR or from within eclipse. Thought?

我正在尝试构建一个将资源文件构建到 jar 中的应用程序,但我希望该项目可以在 eclipse 中运行。我的项目有一个基本的 maven 2 结构,但我不确定如何读取文件,以便在从 JAR 或 Eclipse 中运行时找到并使用它。想法?

Structure:

结构:

src/main/java
src/main/resources/file.txt

Current reading method:

目前的阅读方法:

getClass().getResourceAsStream("/file.txt")

Is there reading method that will pick up src/main/resources/*, as well as the root level of the JAR (where resources are deployed)?

是否有读取 src/main/resources/* 以及 JAR 的根级别(部署资源的位置)的读取方法?

回答by Pascal Thivent

I don't understand your problem. Resources from src/main/resourcesare automatically copied over to target/classesand are thus available on the classpath under Maven and Eclipse relatively to the root level at the same location (unless your Eclipse project is not properly configured).

我不明白你的问题。资源src/main/resources会自动复制到target/classesMaven 和 Eclipse 下的类路径上,相对于同一位置的根级别(除非您的 Eclipse 项目没有正确配置)。

And when packaged inside a JAR, the content of target/classesis packaged "as is" so nothing is changed.

并且当打包在 JAR 中时,其内容target/classes是“按原样”打包的,因此没有任何更改。

In other words, accessing your file.txtlike this is perfectly fine (and this is actually how things are documented):

换句话说,file.txt像这样访问你的东西非常好(这实际上是记录事情的方式):

// Retrieve resource
InputStream is = getClass().getResourceAsStream( "/file.txt" );

// Do something with the resource

...

If you have a problem somewhere, please clarify.

如果您在某处有问题,请澄清。

Update:I did a quick test with the maven-eclipse-plugin and I can't reproduce your problem:

更新:我使用 maven-eclipse-plugin 进行了快速测试,但无法重现您的问题:

$ mvn archetype:generate -DgroupId=com.stackoverflow -DartifactId=q2467362 -Dversion=1.0-SNAPSHOT
...
$ cd q2467362
$ mkdir -p src/main/resources
$ mvn eclipse:eclipse
...
$ cat .classpath
<classpath>
  <classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
  <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
  <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
  <classpathentry kind="output" path="target/classes"/>
  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar" sourcepath="M2_REPO/junit/junit/3.8.1/junit-3.8.1-sources.jar"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
</classpath>

The directory src/main/resourcesis added as source folder as expected. Can you show your POM (especially the resourceselement if you define one)?

该目录src/main/resources按预期添加为源文件夹。你能展示你的 POM(尤其是resources你定义的元素)吗?

回答by Vinny

Anything that is put in src/main/resources using maven2 will be placed in the root level of the jar. So the method you are currently using will satisfy both conditions. From the Maven page: http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR

使用 maven2 放置在 src/main/resources 中的任何内容都将放置在 jar 的根级别中。因此,您当前使用的方法将满足这两个条件。从 Maven 页面:http: //maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR