从jar或者classpath的任何地方读取文件? [英] Reading a file from a jar, or anywhere on the classpath?

查看:202
本文介绍了从jar或者classpath的任何地方读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

结构:

$ s $ / $ $ $ $ b src / main / resources / file.txt

当前阅读方法:

  getClass()。getResourceAsStream(/ file.txt)

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

解决方案

我不明白你的问题。来自 src / main / resources 的资源会自动复制到 target / classes ,因此可以在Maven下的类路径中使用和Eclipse相对于相同位置的根级别(除非您的Eclipse项目没有正确配置)。

当打包到JAR中时, target / classes 的内容是按原样打包的,改变。换句话说,像这样访问你的 file.txt 是非常好的(这实际上是记录事情的方式):

  //检索资源
InputStream is = getClass()。getResourceAsStream(/file.txt);

//使用资源


如果您遇到问题,请澄清。



更新:我使用maven-eclipse-插件和我不能重现您的问题:

$ $ p $ code $ $ mvn archetype生成-DgroupId = com.stackoverflow -dartifactId = q2467362 - Dversion = 1.0-SNAPSHOT
...
$ cd q2467362
$ mkdir -p src / main / resources
$ mvn eclipse:eclipse
...
$ cat .classpath
< classpath>
< classpathentry kind =srcpath =src / main / javaincludes =** / *。java/>
< classpathentry kind =outputpath =target / classes/>
< classpathentry kind =conpath =org.eclipse.jdt.launching.JRE_CONTAINER/>
< / classpath>

目录 src / main / resources 是按预期方式添加为源文件夹。你可以显示你的POM(特别是 resources 元素,如果你定义的话)?


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?

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)?

解决方案

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).

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

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

// 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:

$ 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. Can you show your POM (especially the resources element if you define one)?

这篇关于从jar或者classpath的任何地方读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆