user2152898 user2152898 - 1 year ago 68
Java Question

How to read a properties file outside my webapp context in Tomcat

I have this requirement that my webapp needs to read from a properties file outside the Tomcat webapps directory, for example from $CATALINA_HOME/properties/ I have tried several things but so far no luck

Answer Source

There are multiple approaches .

  1. Use an Environmental variable
  2. Use a System Property
  3. Set it as a Application Context Param in Web.xml

Heres a sample ,that showsOption 1 and Option 2

try {

    //Use Any Environmental Variable , here i have used CATALINA_HOME
    String propertyHome = System.getenv("CATALINA_HOME");           
    if(null == propertyHome){

        //This is a system property that is  passed
        // using the -D option in the Tomcat startup script
        propertyHome  =  System.getProperty("PROPERTY_HOME");

    String filePath= propertyHome+"/properties/";

    Properties property = new Properties();         
} catch (IOException e) {
    // TODO Auto-generated catch block
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download