Java 如何解决在使用Spring加载.properties文件时找不到文件的问题?

Java 如何解决在使用Spring加载.properties文件时找不到文件的问题?,java,spring,Java,Spring,我在使用Spring读取migration.properties时收到“FileNotFoundException” 文件目录: 迁移->src->配置 堆栈跟踪: `Caused by: java.io.FileNotFoundException: class path resource [config/migration.properties] cannot be opened because it does not exist at org.springframework.core.io

我在使用Spring读取migration.properties时收到“FileNotFoundException”

文件目录: 迁移->src->配置

堆栈跟踪:

`Caused by: java.io.FileNotFoundException: class path resource [config/migration.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:142)
... 8 more`
您的文件可能不在您认为的位置。 我建议你打开文件夹,仔细检查文件的路径。我经常在我的mac电脑上简单地点击“复制路径到文件”来让自己更容易

您在此处描述的路径: 迁移->src->配置 会是

 /migration/src/config/migration.properties 
如果文件名为migration.properties


另一个问题可能是/Migration/src/config文件夹需要位于类路径中。(原来这就是问题所在)。

看起来migration.properties不在类路径中。您可以使用文件://给出绝对路径并加载它。

迁移是项目名称,然后我有src文件夹,在src文件夹下我有文件所在的配置文件夹。我强烈建议您右键单击该文件,只需执行“将路径复制到文件”。似乎有人也否决了我的答案。不确定完全限定名是什么,但它不工作完整路径是什么?当你把它放进完整的路径时,你会得到什么样的异常呢?我在类路径中添加了它,现在它开始工作了。我接受了你的回答。非常感谢您的时间。总之,您让它在特定目录下的类路径上查找文件,但没有将该目录放在类路径上?
 /migration/src/config/migration.properties