Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
Asked Answered
R

21

19

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

Runagate answered 15/4, 2019 at 10:47 Comment(3)
Please post the code you have written, or a minimal, verifiable example of the code. Also, please format your question so it is more readable.Litterbug
Make sure the application.properties file is in your classpath. If you use maven, it should be in src/main/resources directory.Creeper
Post your project structure screenshot.Proteus
M
24

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.

Miff answered 28/9, 2021 at 9:15 Comment(2)
that fixed it for me too while everything else didn't! :)Wolof
This suggestion helped me get rid of a similar error. Thank you.Phenocryst
D
11
  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

Docilla answered 13/6, 2019 at 10:14 Comment(1)
Thanks for the clue. I have renamed the properties file but forgot to change in the pom file. You saved my time ;-)Griggs
B
3

Mark it as root resource.

Here is how it's done in intellij:

enter image description here

Buffum answered 30/6, 2020 at 16:47 Comment(0)
P
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>
Perturb answered 5/12, 2020 at 21:3 Comment(0)
P
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

Pilcher answered 10/9, 2022 at 4:37 Comment(0)
R
1

Move the application.properties file to the resources directory.

Razzia answered 16/4, 2019 at 10:59 Comment(0)
F
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

Fortissimo answered 14/5, 2020 at 6:0 Comment(0)
J
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.sstatic.net/pyvTk.png

Jinja answered 29/1, 2021 at 8:55 Comment(0)
R
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>
Ruella answered 1/4, 2020 at 12:9 Comment(0)
D
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

Demolition answered 5/5, 2021 at 11:35 Comment(0)
K
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.

Kenakenaf answered 27/8, 2021 at 23:1 Comment(0)
G
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>
Gunk answered 17/3, 2022 at 6:30 Comment(0)
P
0

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

enter image description here

Pilcher answered 18/9, 2022 at 1:5 Comment(0)
G
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

Glossectomy answered 24/10, 2022 at 17:42 Comment(0)
S
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.

Stuck answered 10/7, 2023 at 6:6 Comment(0)
D
0

I solved the issue by using this file structure.

public class App {
public static void main(String[] args) {
    System.out.println("Hello World!");
    ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
    Student student1 = (Student) context.getBean("student1");
    System.out.println(student1); // override toString()
  }
}

Output:

Hello World!
Student{studentId=2233, studentName='Rahul', studentAddress='Chennai'}

Drawing answered 11/10, 2023 at 12:4 Comment(0)
B
0

You may need to export a profile in order for spring-boot to find it's config props.

 export spring_profiles_active=local

Check what profiles are defined for the project.

Buffum answered 15/10, 2023 at 3:20 Comment(0)
P
0

I have this problem after I add

   <includes>
                <include>**/*.p12</include>
                <include>**/*.pem</include>
                <include>**/*.jks</include>
   </includes>

to a resource in pom file

Pachysandra answered 27/5, 2024 at 3:18 Comment(0)
O
0

Remove project from IDE, delete .settings/.idea folder, re-import the project to IDE.

Omidyar answered 4/7, 2024 at 12:47 Comment(0)
M
0

I got the same problem while running my application. Later I fixed it, but the resource folder's files are misplaced in your project folder. Please cross-verify the application.properties file under the project/src/main/resources/ folder.

Moderator answered 5/7, 2024 at 13:8 Comment(0)
A
-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.

Anadem answered 26/9, 2019 at 11:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.