How to read Properties File in WebSphere 8.5 and JBoss 7

No Comments

If you need to access a properties file located outside your application, so you can change the properties without the need of a redeployment, here’s how you can do it in both WebSphere 8.5, and JBoss 7.

WebSphere 8.5

The first thing we need to do is to place these files inside the server, assuming we are on Linux, we can place these files in:

1
/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/properties/myprop

Note that AppSrv01 is the name of a profile so that could be different for you.

The second thing we need to do is to create a new Shared Library, and this is done using the IBM Console.

Go to Environment and then Shared libraries.

WebSphere Jboss Properties File

The following page will contain all the shared libraries, to create a new one, simply click on New.

WebSphere Jboss Properties File

Give the library a Name, and set the Classpath, then click on Apply.

WebSphere Jboss Properties File

Now the Shared Library has been created, we need to configure the application to use this Library so we can access the Properties File of this Library inside our application.

To do this, go to Applications then WebSphere enterprise applications.

WebSphere Jboss Properties File

Click on the desired application, for this tutorial it will be DefaultApplication.

WebSphere Jboss Properties File

In the next page, and under References, select Shared library references.

WebSphere Jboss Properties File

Now for each item, mark the checkbox, and click on Reference shared libraries.

WebSphere Jboss Properties File

Select the library, and click on the right arrow button, to include it in the list of chosen libraries, and click OK.

WebSphere Jboss Properties File

This will be the final result, click on OK.

WebSphere Jboss Properties File

You may need to restart the application. Skip to Code

JBoss 7

In JBoss, we need to create a new module, which is actually a group of folders and a single file.

First we need to create the following folders inside the modules folder.

WebSphere Jboss Properties File

1
{JBOSS_HOME_DIRECTORY}/modules/com/companyName/projectName/main

Inside the main folder, place your properties file, then create an empty document, name it module.xml, and add the following inside it:

1
2
3
4
5
6
?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.companyName.projectName">
    <resources>
        <resource-root path="."/>
    </resources>
</module>

And your new module is ready to be used, one more file is needed inside your application which simply commands your application to use this module.

Create an empty document, name it jboss-deployment-structure.xml and place it inside META-INF or WEB-INF in case it’s a web application, and add the following inside it:

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8"?>  
<jboss-deployment-structure>  
  <deployment>  
    <dependencies>  
      <module name="com.companyName.projectName" />  
    </dependencies>  
  </deployment>  
</jboss-deployment-structure>

You may need to restart your server, or redeploy your application.

The Code

Either you’re using WebSphere and you have created a Shared Library, or you are using JBoss and you just created the module, you can read your properties file using the following piece of code:

1
2
3
4
InputStream inputStream  = this.getClass().getClassLoader().getResourceAsStream("myfile.properties");
Properties prop = new Properties();
prop.load(inputStream);
String value = prop.get("key1").toString();
Categories: Java Tags: Tags: , , ,

Leave a Reply