Saturday, April 24, 2010

Using property files in Java web applications

The use of property files avoids hardcoding content in Java classes. One way of using property file is loading it as a normal file with absolute file paths, which is not optimal. The other way, which involves classloaders, we avoid putting environment specific details into our code. This way, we make our code more portable.

Let us create an example property file

name=John Doe
description=John Doe is a \
Java software developer

The property file contains key value pairs separated by '='. When the value is spread among multiple lines we use a backslash to indicate the content continues on the next line. Normally, the property file is put in the CLASS_PATH for the classloader to be found. In Java web applications, the file is stored in WEB-INF/classes. In some cases you want to store the file in the same directory as the class that loads it. In that case, use the relative path (the full package name) to load it. In the example below, the property file is stored in com/package/.

private static final String PROPERTIES_FILE =

The path to the property file is defined as a static variable in the class that uses it. The class loads the properties in the following way:

Properties properties = new Properties();
String name = props.getProperty("name");
String email = props.getProperty("email");
String description = props.getProperty("description");


  1. Hello! In case you need to make your web app multilingual, I suggest to evaluate a software localization tool like POEditor to simplify and improve your workflow.

  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java EE Online Training from India. or learn thru Java EE Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.

  3. Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.
    Java training in Chennai

    Java training in Bangalore