Albin Chandy
Albin Chandy

Reputation: 59

Configuration.properties (The system cannot find the file specified)

I have created a spring-boot application with a Configuration.properties file in the classpath. But when i package it and run the application I get the error - "Configuration.properties (The system cannot find the file specified)." Invoked it in the following method : public static Properties readProperties() throws IOException {

    FileReader reader=new FileReader("Configuration.properties");
    Properties p=new Properties(); 
    p.load(reader);
    return p;

I am new to spring,spring-boot.Is there a way to give the values inside application.properties(inside resources folder) of the spring-boot app. Or why does the properties file I have created doesnt go into the jar(upon packaging). Can someone please help.

Regards, Albin

Upvotes: 0

Views: 4409

Answers (1)

user911651
user911651

Reputation:

It is because the file is packaged in your jar file and is not a regular file on the file system. If you want to access a file from the classpath you should access it the following way:

Properties properties = new Properties();
try (InputStream stream =
       this.getClass().getResourceAsStream("/Configuration.properties")) {
    properties.load(stream);
}

However, using Spring Boot you usually would not read a properties file directly unless you really need it for some reason. There are other mechanisms to achieve this. Have a look at the Spring docs here.

Upvotes: 2

Related Questions