How to read Properties File in WebSphere 8.5 and JBoss 7
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.
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:
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.
The following page will contain all the shared libraries, to create a new one, simply click on New.
Give the library a Name, and set the Classpath, then click on Apply.
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.
Click on the desired application, for this tutorial it will be DefaultApplication.
In the next page, and under References, select Shared library references.
Now for each item, mark the checkbox, and click on Reference shared libraries.
Select the library, and click on the right arrow button, to include it in the list of chosen libraries, and click OK.
This will be the final result, click on OK.
You may need to restart the application. Skip to Code
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.
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.
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();