The /WEB-INF
folder is not part of the classpath. So any answer here which is thoughtless suggesting ClassLoader#getResourceAsStream() will never work. It would only work if the properties file is placed in /WEB-INF/classes
which is indeed part of the classpath (in an IDE like Eclipse, just placing it in Java source folder root ought to be sufficient).
Provided that the properties file is really there where you'd like to keep it, then you should be getting it as web content resource by ServletContext#getResourceAsStream() instead.
Assuming that you're inside a HttpServlet
, this should do:
properties.load(getServletContext().getResourceAsStream("/WEB-INF/properties/sample.properties"));
(the getServletContext()
is inherited from the servlet superclass, you don't need to implement it yourself; so the code is as-is)
But if the class is by itself not a HttpServlet
at all, then you'd really need to move the properties file into the classpath.
See also:
- Where to place and how to read configuration resource files in servlet based application?