14

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

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
escort
  • 147
  • 1
  • 1
  • 10

16 Answers16

14

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.

Hesam Karimian
  • 171
  • 1
  • 5
10
  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

Abdur Rahman
  • 1,420
  • 1
  • 21
  • 32
  • 1
    Thanks for the clue. I have renamed the properties file but forgot to change in the pom file. You saved my time ;-) – vkrams Aug 17 '19 at 09:43
2

Mark it as root resource.

Here is how it's done in intellij:

enter image description here

z atef
  • 7,138
  • 3
  • 55
  • 50
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>
harun ugur
  • 1,718
  • 18
  • 18
2

If you use IntelliJ Idea

If you declared your classpath like this:

@PropertySource("classpath:sport.properties")
public class SportConfig {
 //some code
}

Then you Should add your properties file to the resources directory

ilia
  • 98
  • 1
  • 8
1

Move the application.properties file to the resources directory.

Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
1

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.You can follow bellow picture for the same

1

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.

https://i.stack.imgur.com/pyvTk.png

0
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>
dcba dcba
  • 106
  • 1
  • 3
0

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

0

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.

shix
  • 57
  • 11
0

enter image description here

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>
Supun Sandaruwan
  • 1,814
  • 19
  • 19
0

If you use Eclipse IDE You should create a "resources" folder inside src. Then It will work.

enter image description here

ilia
  • 98
  • 1
  • 8
0

If you get this error in case of switching from/to .yml and/or .properties file, you might have to also check if you have explicitly mentioned which properties(.properties or .yml) file you explicitly mentioned in configuration file. Search application.properties or .yml and look for @PropertiesSource value if it is mentioned in there

Tadele Ayelegn
  • 4,126
  • 1
  • 35
  • 30
0

Run commands mentioned below one by one -

  1. "mvn clean"
  2. "mvn clean install -am -pl -DskipTests"

Go to your project refresh and build.

Priyanshu
  • 1
  • 1
-1

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.

Booboo
  • 38,656
  • 3
  • 37
  • 60