How to get context path to properties file from webservice in java?

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 ...

EDIT: I do not use servlets ...

+3
source share
3 answers

, , Class#getResourceAsStream() . /WEB-INF/classes , API , . /WEB-INF/resources .

IDE, Eclipse, , ( /WEB-INF/classes). , resources Java, . .

+2

/WEB-INF/:

"/WEB-INF/messages/myfile";
0

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");
0
source

All Articles