Search code examples
javajspnullpointerexceptionicefacesfilenotfoundexception

Issue when getting value from build.properties in ICEfaces


I want to get the value of a property I added from build.properties

dergilik.host=http://172.171.1.155/

with the code below

private final static String PROPERTIES_FILE = "build.properties";
private final static String HOST = "dergilik.host";
private final Properties props = new Properties();

private String getHost() {
    try {
        InputStream inp = new FileInputStream(PROPERTIES_FILE);
        props.load(inp);
        return props.getProperty(HOST);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        System.out.println(e.getMessage());
        e.printStackTrace();
        return null;
    }        
}

But the application throws FileNotFoundException when props.load(inp);. I also tried it with ClassLoader class and NullPointerException occured. Furthermore I changed PROPERTIES_FILE as "/ProjectName/build.properties" but it didn't work.

So how can I make the program correctly find build.properties file?

Thanks


Solution

  • If you want to access in web application

    • and the resource is inside web application and you have access to ServletContext, you should use ServletContext.getResourceAsStream().
    • and the resource is outside web application, configure the path using context-param in web.xml and try accessing using ServletContext.getInitParameter()

    It if is not Web application and present in the classpath use,

    • Thread.currentThread().getContextClassloader().getResourceAsStream()