2

I'm trying to load a configuration file. But it doesn't work my configuration file is placed under WEB-INF folder

and here is my code to load that conf file :

private static final String PROPERTIES_FILE = "/WEB-INF/dao.properties";
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
        InputStream fichierProperties = classloader.getResourceAsStream(PROPERTIES_FILE);

        if (fichierProperties == null) {
            throw new DAOConfigurationException("file "+PROPERTIES_FILE+ " not found" );
        }

I'm always getting this error file not found, Should make some changes on the build path ??

Stranger B.
  • 9,004
  • 21
  • 71
  • 108

2 Answers2

1

For simple purpose, try

Put dao.properties inside src folder (where put source code).

Change to

private static final String PROPERTIES_FILE = "dao.properties";  //  <-------
ClassLoader classloader = Thread.currentThread().getContextClassLoader();    
InputStream fichierProperties = classloader.getResourceAsStream(PROPERTIES_FILE);

if (fichierProperties == null) {
    throw new DAOConfigurationException("file "+PROPERTIES_FILE+ " not found" );
}
Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • Thanks for your answer ! but my configuration file will be in a safe place in the src folder ? – Stranger B. Jun 10 '16 at 01:24
  • use `classpath:` (you search Google for example use `classpath` with keyword: `java classpath properties`). I looking for a nice example for you: http://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/ and http://stackoverflow.com/questions/9663564/how-to-load-files-properties-from-web-inf-directory?lq=1 – Vy Do Jun 10 '16 at 01:38
1

If you put your file inside the WEB-INF directory, You can use context object to read your file as shown if you have access to servlet context

InputStream input = context.getResourceAsStream("/WEB-INF/dao.properties");
KItis
  • 5,476
  • 19
  • 64
  • 112