Given that fileName
is /WEB-INF/classes/myapp.properties
, you need to get it as a webapp resource, not as a local disk file system file.
So, replace
String fileName = config.getInitParameter("configFile");
System.out.println(fileName);
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
p = new Properties();
p.load(fis);
by
String fileName = config.getInitParameter("configFile");
InputStream input = config.getServletContext().getResourceAsStream(fileName);
p = new Properties();
p.load(input);
A simpler way is to set the fileName
to myapp.properties
and get it as a classpath resource.
String fileName = config.getInitParameter("configFile");
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
p = new Properties();
p.load(input);
See also:
- getResourceAsStream() vs FileInputStream
- Where to place and how to read configuration resource files in servlet based application?