25 May 2005

Loading the Properties with Classpath resource

This technique is the most taken-for-granted technique by some advanced users. But I see some developers who still use the java.io.File when loading properties which is very difficult or even impossible to adapt when your APIs are being called from stand-alone to web applications or even to the application server's lifecycle container. So much for talk, I'll show you a 10-minute code on how to do this.

Listing 1: Is the properties loader class called PropertiesLoader.java



/*
* Created on May 25, 2005
*
*/
package props.sample;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

/**
* Shows how to load the properties with classpath resource
*
* @author Jared Odulio
*
*/
public class PropertiesLoader {

private Properties props;
private String property;

/**
*
*/
public PropertiesLoader() {

initialize();

}

private void initialize(){

try {

props = new Properties();
InputStream is = this.getClass().getResourceAsStream("/conf.properties");
props.load(is);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
* Returns the property value from a given key.
*
* @param key
* @return
*/
public String getProp(String key){

property = props.getProperty(key);
return property;

}

/**
* Lists the property value to the output stream.
*
*/
public void getProps(){

props.list(System.out);


}
}




Note the class uses the getResourceAsStream() that looks up the application's classpath.

Listing 2: The main executable, MainProps.java



/*
* Created on May 25, 2005
*
*/
package props.sample;

/**
*
* The main executable.
*
* @author Jared Odulio
*
*
*/
public class MainProps {

public static void main(String[] args) {

PropertiesLoader pl = new PropertiesLoader();
System.out.println("Getting a property key = my.key:");
System.out.println("Property is " + pl.getProp("my.key") + "\n\n");

pl.getProps();

}
}



From here, we simply use the PropertiesLoader class.

Listing 3: The properties file called conf.properties



my.key=this_is_the_value
my.anotherkey=anothervalue



To execute this program we simply specify the location of the conf.properties to the classpath either by modifying the CLASSPATH environment variable or specifying the JVM arguments at runtime by doing this:



java -cp <LOCATION_OF_THE_PROPERTIES_FILE> props.sample.MainProps



The result will be:



Getting a property key = my.key:
Property is this_is_the_value


-- listing properties --
my.key=this_is_the_value
my.anotherkey=anothervalue



This technique has been proven to work anywhere whether it's stand-alone application, web or distributed application sitting on top every J2EE compliant application servers like Sun App Server, Weblogic, Websphere, JoNaS, JBoss etc. etc.

1 comment:

Tabitha said...

hi,
Thanks a lot for for the code. I was struggling to load the properties file from JBoss.