I have my .properties file in
com.someOtherpage
-somefolder
--theProperties.java `<--- This guy needs it`
com.somepackage
WEB-INF
-config
--project.properties `<--- Here is where he sits`
when deployed how can I call the properties file with out calling its absolute path like below
public class theProperties
{
private static Properties properties = new Properties();
public theProperties()
{
}
public String get(String attribute) throws Exception
{
//what do I need to set up to be able to call this file this way
//notice there is no '../../project.properties'
// -----
InputStream is = theProperties.class.getResourceAsStream("project.properties");
properties.load(is);
is.close();
return properties.getProperty(attribute);
}
}
The above isn’t working, why?
If you put the
propertiesfile in the same package as the Class that reads it you specify its path relative to that class, that is if the properties file is in the exact same package as the class loading it you specify the path asproject.properties.If you put the properties file in the default package and the loading class isn’t in the default package, you have to specify an absolute path like,
/project.properties. Just a reminder no classes should be in the default class path as a general rule.Either way, your properties file has to be on the
classpathwhich yours isn’t. In other words it has to be somewhere inWEB-INF/classes/.A better solution, but more complex is to use Guice to inject properties and not write your own reader.