<rdf:RDF
    xmlns:s='http://snipsnap.org/rdf/snip-schema#'
    xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
    xml:base='http://knowhow.amazers.net/rdf'>
    <s:Snip rdf:about='http://knowhow.amazers.net/rdf#dev/java+tips/Loading+properties+in+a+web+application'
         s:cUser='maz'
         s:oUser='maz'
         s:mUser='maz'>
        <s:name>dev/java tips/Loading properties in a web application</s:name>
        <s:content>1 Loading properties files in a web application&#xD;&#xA;Some times it is needed to load configuration data from properties files.&#xD;&#xA;In a standard java application this could be done via&#xD;&#xA;{code:type=java}&#xD;&#xA;  InputStream is = new FileInputStream(&quot;conf/application.properties&quot;);&#xD;&#xA;  java.util.Properties prop = new Properties();&#xD;&#xA;  prop.load(is);&#xD;&#xA;  is.close();&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;The file is loaded relativ from the start directory. In a webapp container (i.e. Tomcat) the start directory is not determined and is mainly the directory where the startup script has been called. So if you directly start tomcat from the CATALINA_HOME/bin so this will also be the start directory. But one could also navigate to CATALINA_HOME and start bin/startup. So CATALINA_HOME will be the start directory.&#xD;&#xA;&#xD;&#xA;OFF-Topic: My suggestion is to start/stop tomcat by your own scripts, which change the current directory to CATALINA_HOME or if available to CATALINA_BASE and call from there bin/startup or CATALINA_HOME/bin/startup. &#xD;&#xA;&#xD;&#xA;For some applications and for webapps in general it is a good idea to load the configuration files from the classpath, so they could be placed in WEB-INF/classes or within one of the jars.&#xD;&#xA;&#xD;&#xA;So it would like this:&#xD;&#xA;{code:type=java}&#xD;&#xA;  URL url = ClassLoader.getSystemResource(&quot;conf/application.properties&quot;);&#xD;&#xA;  java.util.Properties prop = new Properties();&#xD;&#xA;  prop.load(url.openStream());&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;But in Tomcat this won&apos;t work because of the classloader hierachy in tomcat. Every webapp has it&apos;s own classloader in tomcat. The ClassLoader.getSystemResource will use the system class loader that is used to load the bootstrap and the tomcat application. But the system class loader does not know anything about your webapp and it&apos;s classpath. Using the right class loader is essential. There are a lot of solutions to access the correct classloader (i.e. this.class.getClassLoader()). The following example will also show how to do the trick within static methods:&#xD;&#xA;&#xD;&#xA;{code:type=java}&#xD;&#xA;public class Config {&#xD;&#xA;     private static java.util.Properties prop = new java.util.Properties();&#xD;&#xA;     private static void loadProperties() {&#xD;&#xA;          // get class loader&#xD;&#xA;          ClassLoader loader = Config.class.getClassLoader();&#xD;&#xA;          if(loader==null)&#xD;&#xA;            loader = ClassLoader.getSystemClassLoader();&#xD;&#xA;&#xD;&#xA;          // assuming you want to load application.properties located in WEB-INF/classes/conf/&#xD;&#xA;          String propFile = &quot;conf/application.properties&quot;;&#xD;&#xA;          java.net.URL url = loader.getResource(propFile);&#xD;&#xA;          try{prop.load(url.openStream());}catch(Exception e){System.err.println(&quot;Could not load configuration file: &quot; + propFile);}&#xD;&#xA;     }&#xD;&#xA;&#xD;&#xA;     //....&#xD;&#xA;     // add your methods here. prop is filled with the content of conf/application.properties&#xD;&#xA;&#xD;&#xA;     // load the properties when class is accessed&#xD;&#xA;     static {&#xD;&#xA;        loadProperties();&#xD;&#xA;     }&#xD;&#xA;  }&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;This method even works in a standalone java application without any modification. So it is my preferred way. &#xD;&#xA;&#xD;&#xA;&#xD;&#xA;</s:content>
        <s:mTime>2010-05-15 20:59:23.803</s:mTime>
        <s:cTime>2010-05-15 20:57:51.398</s:cTime>
        <s:comments
             rdf:type='http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag'/>
        <s:snipLinks>
            <rdf:Bag>
                <rdf:li rdf:resource='http://knowhow.amazers.net/rdf#dev/java tips'/>
                <rdf:li rdf:resource='http://knowhow.amazers.net/rdf#dev/projects/hardware/GardenaBodenfeuchteSensorFS20Modul'/>
                <rdf:li rdf:resource='#snipsnap-index'/>
                <rdf:li rdf:resource='http://knowhow.amazers.net/rdf#dev/java tips/Hacks around XMLGregorianCalendar'/>
                <rdf:li rdf:resource='#snipsnap-search'/>
                <rdf:li rdf:resource='http://knowhow.amazers.net/rdf#dev/projects/hardware/FS20 Magnet Ventil Controller'/>
                <rdf:li rdf:resource='http://knowhow.amazers.net/rdf#dev/projects/hardware/nxt/Tips &amp; Info'/>
                <rdf:li rdf:resource='http://knowhow.amazers.net/rdf#dev/projects/hardware/RFIDAuthXBeeController'/>
                <rdf:li rdf:resource='#snipsnap-about'/>
                <rdf:li rdf:resource='http://knowhow.amazers.net/rdf#dev/java tips/'/>
                <rdf:li rdf:resource='#maz'/>
                <rdf:li rdf:resource='http://knowhow.amazers.net/rdf#dev/projects/hardware/FS20 Liquid Level Sensor'/>
                <rdf:li>
                    <s:Snip rdf:about='http://knowhow.amazers.net/rdf#dev/java+tips/Loading+properties+in+a+web+application'>
                        <s:attachments
                             rdf:type='http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag'/>
                    </s:Snip>
                </rdf:li>
            </rdf:Bag>
        </s:snipLinks>
    </s:Snip>
</rdf:RDF>

