0

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

Albin Chandy
  • 59
  • 1
  • 9
  • [for someone still facing this , check my answer here: Its working for me](https://stackoverflow.com/questions/47922428/executing-as-jar-says-system-cannot-find-the-properties-file-specified/64279958#64279958) – Vikash Kumar Oct 09 '20 at 12:24

1 Answers1

2

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.

AlexLiesenfeld
  • 2,872
  • 7
  • 36
  • 57