To read properties file outside .ear file in J2EE application is very useful. You can easily change the value of properties file without rebuild your ear file.

Here is the example how to read properties file outside ear file in JBoss application server. I put my .ear file in $Jboss_Root/server/default/deploy/myapp.ear and my properties file in the same level of my .ear file $Jboss_Root/server/default/deploy/myapp.properties

public void getProp() {
	String baseDir = System.getProperty("jboss.server.base.dir");
	String slash = System.getProperty("file.separator");
	String url = basDir + slash +"default"+slash+"deploy"+slash+"myapp.properties";
	try {
		FileInputStream fis = new FileInputStream(url);
		Properties prop = new Properties();
		prop.load(fis);
		System.out.println(prop.get("mykeyword"));
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

In the example above, the logic is read the properties file using full path. Since i want to put my properties file inside jboss’s folder (/deploy) and i dont want to hard coded my jboss installation folder, so the wise way is to get jboss installation folder using jboss keyword

jboss.server.base.dir

To find out other jboss properties check here.

Another important thing to be considered is

file.separator

This is to avoid specific OS file separator. As we know M$ and *nix uses different file separator, which is / and \

One Response to “How to read properties file outside ear file”

  1. Leo Says:

    Excellent article, I was looking for that!

Leave a Reply