简体   繁体   English

为什么执行时不能读取/ src / main / resources下的文件?

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

相关问题 无法访问src / main / resources /下的文件 - Can't get to a file located under src/main/resources/ 为什么我的java swing操作无法在src / main / resources中找到我的图像? - Why can't my java swing action find my image in src/main/resources? 为什么Maven不包括我的文件/src/main/resources/config.props? - Why is Maven not including my file /src/main/resources/config.props? src / main / resources中的文件不在classpath中 - file in src/main/resources is not in classpath 如何让SpringBoot在src/main/resources目录下找到我的文件 - How to get SpringBoot to find my file in the src/main/resources directory Java 无法访问 src/main/resources 中的 txt 文件 - Java can't access txt file in src/main/resources 我的资源文件夹中的文件未使用 try-with-resources 和 BufferedWriter 写入,为什么? - A file in my resources folder isn't getting written with try-with-resources and BufferedWriter, why? 为什么即使我的xml文件在maven项目的src / main / resources中,也找不到hibernate.cfg.xml? - Why I get hibernate.cfg.xml not found even if my xml file is in src/main/resources in maven project? 每次更改src / main / resources下的属性文件时,都必须重新构建Jar - I have to rebuild the Jar everytime I change the property file under src/main/resources Springboot - application-integration.properties 在 src/main/resources 下工作,但不在 src/test/resources 下工作 - Springboot - application-integration.properties working under src/main/resources, but not under src/test/resources
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM