You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Apache Wiki <wi...@apache.org> on 2010/05/15 01:53:32 UTC
[Tomcat Wiki] Update of "HowTo" by hotmaz
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Tomcat Wiki" for change notification.
The "HowTo" page has been changed by hotmaz.
The comment on this change is: added a new method for loading properties from WebApp by Maz.
http://wiki.apache.org/tomcat/HowTo?action=diff&rev1=88&rev2=89
--------------------------------------------------
See AntDeploy
== How do I load a properties file? ==
- Here are the two most popular ways::
+ Here are the three most popular ways::
+ * Use a classloader's getResource to get an url to the properties file and load it into the Properties. The properties file must be located within the webapp classpath (i.e. either WEB-INF/classes/... or in a jar.
+ A challenge is to get the classloader when you are in a static initializer:
+ {{{
+ public class Config {
+ private static java.util.Properties prop = new java.util.Properties();
+ private static loadProperties() {
+ // get class loader
+ ClassLoader loader = Config.class.getClassLoader();
+ if(loader==null)
+ loader = ClassLoader.getSystemClassLoader();
+
+ // assuming you want to load application.properties located in WEB-INF/classes/conf/
+ String propFile = "conf/application.properties";
+ java.net.URL url = loader.getResource(propFile);
+ try{prop.load(url.openStream());}catch(Exception e){Syste,.err.println("Could not load configuration file: " + propFile);}
+ }
+ }
+ }}}
+
+ This method even works in a standalone java application. So it is my preferred way.
* Use a `ResourceBundle`. See the Java docs for the specifics of how the `ResourceBundle` class works. Using this method, the properties file must go into the `WEB-INF/classes` directory or in a jar file contained in the `WEB-INF/lib` directory.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org