Reading Properties File from outside of a JAR File

Today in my project I had a requirement to fetch the properties file from out side of my JAR file. Its easy to pick it from the CLASSPATH of that JAR file but not that much easy to pick from a “Special location” like TOMCAT home, outside the JAR file. At first I tried to read it as a ResourceBundle. But with our so much difference I done it as follows…

Sorry, didn’t clarify that the above will work not just with WAS (IBM’s WebSphere Application Server) but any J2EE-standard app server (it will support the J2EE Resource Manager APIs, which came in J2EE 1.3, many years ago, and URL is one of the standard types).

The java.net.URL class is the best overall way to access Properties files and other files outside your .ear. Here’s a bullet chart I teach on my consulting engagements:

In Love with Properties files?

* Whitepaper: “Using URL resources to manage J2EE property files in IBM WebSphere Application Server V5” available at http://www.ibm.com/developerworks
– Yes, an older article but it still works fine
* In short, you configure (in WAS) a URL resource and use it to access the properties file
* You cannot simply read your Properties file (even tho I bet you do)
– J2EE explicitly forbids portable applications from reading files using the java.io package.
– There is the more subtle problem of where to place this file so it can be read by the application code at run time.

The java.net.URL class is the best overall way to access Properties files and other files outside your .ear. Here’s a bullet chart I teach on my consulting engagements:

In Love with Properties files?

* Whitepaper: “Using URL resources to manage J2EE property files in IBM WebSphere Application Server V5”
Yes, an older article but it still works fine
In short, you configure (in WAS) a URL resource and use it to access the properties file
You cannot simply read your Properties file (even tho I bet you do)
J2EE explicitly forbids portable applications from reading files using the java.io package.
There is the more subtle problem of where to place this file so it can be read by the application code at run time.

My problem is the resource file is available in one context (say aa), I’m trying to load the same using ResourceBundle from another context (say bb). But always I get MissingResourceException exception. But it works well when I put the properties file under “bb” context… I think I can use the solution that you mentioned in this post for the time being. But I’m searching for a solution so that I can use ResourceBundle(since the property file is for i18n purposes). Any way thanks dude it is indeed a good post.