[英]Why isn't my file under /src/main/resources readable on execution?
I have a Java project and used the standard maven archetype to create the dir structure. 我有一个Java项目,并使用标准的maven原型来创建dir结构。 It looks like this: 它看起来像这样:
|-src/main/java
|-src/main/resources
|-target/classes
|- ...
Now one of my classes uses a .properties file to read in some settings. 现在,我的一个类使用.properties文件来读取某些设置。 I placed it in src/main/resources and read it through File propertiesFile = new File("./src/main/resources/starter.properties");
我将它放在src / main / resources中并通过File propertiesFile = new File("./src/main/resources/starter.properties");
读取它File propertiesFile = new File("./src/main/resources/starter.properties");
. 。
When I use the eclipse run-configuration, everything works fine. 当我使用eclipse运行配置时,一切正常。 But recently I tried to start the same Java-class from my console using java some.package.Class
, and since the .class-file is located in target/classes I got the message, that ./src/main/resources/starter.properties couldn't be found. 但是最近我尝试使用java some.package.Class
从我的控制台启动相同的Java类,并且由于.class文件位于target / classes中,我得到了消息,即./src/main/resources/starter .properties无法找到。
What am I doing wrong? 我究竟做错了什么? Is the .properties file not supposed to be located in the resources-folder or do I have to use an other way to load it? .properties文件不应该位于resources-folder中,还是我必须使用其他方式加载它?
The two previous answers are correct, but I wanted to give a bit more context. 前两个答案是正确的,但我想提供更多的背景。
This file is in two places. 这个文件分两个地方。 It starts off in /src/main/resources and when you build the project, Maven copies it to /target/classes. 它从/ src / main / resources开始,当你构建项目时,Maven将它复制到/ target / classes。
At runtime, you shouldn't access the copy that is in your source code. 在运行时,您不应该访问源代码中的副本。 Otherwise, your software would need access to the source code in order to run. 否则,您的软件需要访问源代码才能运行。 Rather, you should access the copy that is in your deliverable. 相反,您应该访问交付物中的副本。 At execution time, you can safely assume that you will find it on the classpath. 在执行时,您可以安全地假设您将在类路径中找到它。 It's in the same place as your compiled classes, so if it weren't on the classpath, you wouldn't have been able to run the program in the first place. 它与编译的类位于同一个位置,因此如果它不在类路径中,那么您将无法首先运行该程序。 This is why you should use getResourceAsStream()
as mentioned by the other answerers. 这就是你应该使用其他回答者提到的getResourceAsStream()
原因。
(Though for production software, I do recommend Spring's Resource abstraction for accessing these kinds of things.) (虽然对于生产软件,我建议使用Spring的Resource抽象来访问这些东西。)
采用
YourClass.class.getResourceAsStream("/filename.properties");
To expand on the two answers already given, when you build your Maven project the files in src/main/resources are copied into your JAR at the root of the classpath. 为了扩展已经给出的两个答案,当您构建Maven项目时,src / main / resources中的文件将被复制到类路径根目录的JAR中。 If you did a jar tvf *yourjarname*
you would notice there is neither a src/main/java or src/main/resources folder in it. 如果你做了一个jar tvf *yourjarname*
你会发现它里面既没有src / main / java或src / main / resources文件夹。 So trying to manually read from those (now non-existent) paths will fail when you run via JAR. 因此,当您通过JAR运行时,尝试从那些(现在不存在的)路径手动读取将失败。 The other two answers have excellent suggestions, use the getResourceAsStream
method to read in your file. 另外两个答案有很好的建议,使用getResourceAsStream
方法读取你的文件。 Theres even a method on the Properties object that makes this very convenient and easy to use: 甚至是Properties对象上的一个方法,使得它非常方便和易于使用:
URL resourceURL = Thread.currentThread().getContextClassLoader()
.getResource(yourpropertyfilename);
Properties props = new Properties();
props.load(resourceURL.openStream());
At runtime this file will be in your classpath. 在运行时,此文件将位于类路径中。 Use Class.getResourceAsStream()
to access it. 使用Class.getResourceAsStream()
来访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.