How to load resource from jar file packaged in a war file? [duplicate]
Solution 1:
Put it in root of the JAR and get it by context classloader instead of servletcontext.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("test.properties");
// ...
The /WEB-INF
folder convention is specific to WAR files, not to JAR files. Get rid of it. If you really need a separate JAR folder which is to be part of the classpath, use /META-INF
instead.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("META-INF/test.properties");
// ...
Solution 2:
You can access any resource on the standard classpath from a given instance
this.getClass().getResourceAsStream("name");
for example from your "myservlet" class (Bad naming).
getServletContext().getResourceAsStream()
accesses content in the context of the web application base directory.
It seems bad style to incldue a WEB-INF directory in a jar - you would cause confusion. Can't you find a better name?