Properties file location is WEB-INF/classes/auth.properties.
I cannot use JSF-specific ways (with ExternalContext) because I need properties file in a service module which doesn’t have a dependency on a web-module.
I’ve already tried
MyService.class.getClassLoader().getResourceAsStream("/WEB-INF/classes/auth.properties");
but it returns null.
I’ve also tried to read it with FileInputStream but it requires the full path what is unacceptable.
Any ideas?
Several notes:
You should prefer the
ClassLoaderas returned byThread#getContextClassLoader().This returns the parentmost classloader which has access to all resources. The
Class#getClassLoader()will only return the (child) classloader of the class in question which may not per se have access to the desired resource. It will always work in environments with a single classloader, but not always in environments with a complex hierarchy of classloaders like webapps.The
/WEB-INFfolder is not in the root of the classpath. The/WEB-INF/classesfolder is. So you need to load the properties files relative to that.If you opt for using the
Thread#getContextClassLoader(), remove the leading/.The JSF-specific
ExternalContext#getResourceAsStream()which usesServletContext#getResourceAsStream()“under the hoods” only returns resources from the webcontent (there where the/WEB-INFfolder is sitting), not from the classpath.