Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 850
escort Avatar asked Apr 15 '19 10:04

escort


3 Answers

  1. Check that your application.properties file is in the resources directory as picture shown below: enter image description here

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

enter image description here

like image 134
Abdur Rahman Avatar answered Oct 22 '22 14:10

Abdur Rahman


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.

like image 7
Hesam Karimian Avatar answered Oct 22 '22 14:10

Hesam Karimian


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>
like image 2
harun ugur Avatar answered Oct 22 '22 13:10

harun ugur