1

I am using maven web application. The following is my project structure where I have placed my properties file.

enter image description here

The following is my code which I am using to read this file:

public static Properties loadProducerProperty() throws FileException {
     Properties myProperties = new Properties();

     try {
         String resourceName = "newproperties.properties"; // could also be a constant
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
         try (InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
             myProperties.load(resourceStream);
         }


     } catch (FileNotFoundException e) {
         throw new FileException();
     } catch (IOException e) {
         throw new FileException();
     }

     return myProperties;
 }

But I am getting FileNotFound Exception

I have gone through the following link and tried other things also but I am getting the same error :

Cannot load properties file from resources directory

What I am doing wrong here.

Thank you

Community
  • 1
  • 1
Salman Shaikh
  • 575
  • 1
  • 7
  • 24

1 Answers1

2

Add a slash in front of the filename:

String resourceName = "/newproperties.properties"; // could also be a constant
Jens
  • 67,715
  • 15
  • 98
  • 113