简体   繁体   English

从jar或类路径的任何地方读取文件?

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

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

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的根级别(部署资源的位置)?

I don't understand your problem. 我不明白你的问题。 Resources from src/main/resources are automatically copied over to target/classes and 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/classes ,因此可以在Maven和Eclipse下的类路径上相对于同一位置的根级别提供(除非您的Eclipse项目未正确配置)。

And when packaged inside a JAR, the content of target/classes is packaged "as is" so nothing is changed. 当在JAR中打包时, target/classes的内容将“按原样”打包,因此不会更改任何内容。

In other words, accessing your file.txt like 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/resources is added as source folder as expected. 目录src/main/resources按预期添加为源文件夹。 Can you show your POM (especially the resources element if you define one)? 你能展示你的POM(特别是你定义的resources元素)吗?

Anything that is put in src/main/resources using maven2 will be placed in the root level of the jar. 使用maven2放在src / main / resources中的任何内容都将放在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 从Maven页面: http//maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM