I am trying to read the context path of a properties file from my application,
properties.load(this.getClass().getResourceAsStream(path)); import java.util.Properties; public class test1 { public String getValues() { PropertiesFileReader fileReader = new PropertiesFileReader(); Properties prop = fileReader.getProp("/messages/AttachFile.properties"); String out = prop.getProperty("FILE_NAME"); return out; } }
This works when the properties file is under WEB-INF -> classes -> messages -> myfile but when I move this file to another folder, for example WEB-INF -> messages -> myfile, it doesn't seem to get the path ...
WEB-INF -> classes -> messages -> myfile
WEB-INF -> messages -> myfile
EDIT: I do not use servlets ...
, , Class#getResourceAsStream() . /WEB-INF/classes , API , . /WEB-INF/resources .
Class#getResourceAsStream()
/WEB-INF/classes
/WEB-INF/resources
IDE, Eclipse, , ( /WEB-INF/classes). , resources Java, . .
resources
/WEB-INF/:
/WEB-INF/
"/WEB-INF/messages/myfile";
When you say that you are not using servlets, what do you mean? How is this code executed?Basically, when you use servlets, only the WEB-INF / and WEB-INF / lib classes are in the classpath. Thus, you cannot access resources using class loaders. BUT you can access them using ServletContext. Assuming your code runs in Servlet / JSP, you can do the following:
getServletContext().getResourceAsStream("your resource starting from web-application root");