'Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
I am creating an spring boot Batch application. That Batch loads data from postgres and insert into MongoDB. I have written the code , but while running the spring boot application, its shows error that application.properties file is not found. below are error snippets:
'Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist'
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.example.batch.SpringBatchExample]; nested exception is java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
Below are application.properties file contents:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=Test_Data
#logging
logging.level.org.springframework.data=DEBUG
logging.level.=ERROR
logging.level.com.mastercard.refdata.*=DEBUG
#By default, Spring runs all the job as soon as it has started its context.
spring.batch.job.enabled=false
#Chunk Size to save data
spring.chunk.size=200
#Postgres DATASOURCE
spring.datasource.url=jdbc:postgresql://localhost:5432/Company-Test
spring.datasource.username=postgres
spring.datasource.password=test123
spring.datasource.driver-class-name=org.postgresql.Driver
Please let me know why I am getting this issue??
Solution 1:[1]
Check that your application.properties file is in the resources directory as picture shown below:

If you keep your application.properties to other folder (e.g config) as shown in below picture then configure your pom.xml following code:
<build> <resources> <resource> <directory>config</directory> <targetPath>${project.build.outputDirectory}</targetPath> <includes> <include>application.properties</include> </includes> </resource> </resources> </build>
Solution 2:[2]
configure your pom.xml file add resources;
<build>
<finalName>your-project-name</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
<exclude>**/*.class</exclude>
</excludes>
</resource>
</resources>
</build>
Solution 3:[3]
You need to configure the config path (projectBaseName\target\local\config) in the Bootstrap tab in Run Configuration, because all the properties file are placed in the classpath.
Solution 4:[4]
Solution 5:[5]
for me the solution was to re-add the library (project which is a dependency in my case for my main project) to the assembly deployement of the main project I am working on. For some reason Eclipse was not able to detect this project in its class path.
Solution 6:[6]
I had the same issue although all configurations where all right. after a long time I just used mvn clean package command and everything was fine.
Solution 7:[7]
Move the application.properties file to the resources directory.
Solution 8:[8]
Use this on configuration class
@Configuration
@PropertySource("classpath:application.properties")
(OR)
Use this in xml file
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>application.properties</value>
</list>
</property>
</bean>
Solution 9:[9]
instead of using classpath:filepath inside @sql annotation of @sqlgroup use file:filepath It makes java search through the entire folder searching for a file of similar path,worked like a charm for me
Solution 10:[10]
For the record, this also happens when you have opened your project in multiple IDEs. For example, Eclipse does not recognize the configuration files of other IDEs.
If this happens in Eclipse, what we can do is go to project -> "clean..." option. This will clean the build files and re-build the project.
Solution 11:[11]
properties file location: src/student-info.properties
I imported successfully like below.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="file:src/student-info.properties"/>
<bean id="student" class="com.Student">
<property name="name" value="${student.name}"/>
<property name="hobby" value="${student.interestedCourse}"/>
<property name="interestedCourse" value="${student.hobby}"/>
</bean>
</beans>
Solution 12:[12]
My issue is the same. You're trying to connect multiple databases in your project.
In Main Application Run have an Annotation like:
{ { {@PropertySources({@PropertySource("/../resources/datasource_cfg.properties")})}
It's the path to parse when the application is run. Maybe your path isn't correct.
Solution 13:[13]
For a fixed path you can use:
@PropertySource("file:${C:/Development/workspace/Projectname/resources/}/application.properties")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow



