Archive for April, 2010

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 Properties

| April 27th, 2010

jboss.home.dir – The base directory of the jboss distribution – default

$JBOSS_HOME

jboss.home.url – The base url of the jboss distribution – default

$JBOSS_HOME

jboss.lib.url – The url where the kernel jars exist – default

$jboss.home.url/lib

jboss.patch.url – A directory where patch jars exist – default

none

jboss.server.name – The configuration name of the server – default

default

jboss.server.base.dir – The directory where server configurations exist – default

$jboss.home.dir/server

jboss.server.base.url – The url where server configurations exist – default

$jboss.home.url/server

jboss.server.home.dir – The directory for the current configuration – default

I got an executable jar file for small purpose and this jar file need to read some configuration before doing its job.

Since i hate to put properties file inside jar file, the reason is simple, it doesnt give me flexibility when i want to change value in the prop file. This is an approach how to read properties file outside jar file so you dont need to rebuild your jar file everytime you change properties file value.

You just need to put the properties file in same level with the jar file.

public static String getPropValue(String key) {
	String path = Utility.class.getProtectionDomain().getCodeSource().getLocation().toExternalForm();
	String[] urls = path.split("/");
	StringBuffer url = new StringBuffer("/");
	for( int i = 1; i < urls.length -1; i++ ) {
		url.append(urls[i]);
		url.append("/");
	}
	Properties prop = new Properties();
	String value = null;
    try {
    	prop.load(new FileInputStream(url.toString() + "mydumb.properties"));
    	value = prop.getProperty(key);
    }
    catch (IOException e) {
    	e.printStackTrace();
    }
 
    return value;
}