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…

Click here to Read Full Article

10 thoughts on “Reading Properties File from outside of a JAR File

  1. I can’t able to read your article. When I click on the link if gets redirected somewhere. I really want to take a look at this as I believe that you gave a solution for my problem 🙂

  2. Hi All,
    I need to read properties file from an external directory in my JAR file. How can I do this?

  3. 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).

  4. 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.

  5. 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.

  6. Hi Prakash,

    If you are still looking for solution, try this
    ResourceBundle.getBundle(“folder name inside jar”,locale, classloader);

  7. 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.

  8. Hi Lemnik,

    You know friend I always look for your comments. I know there will be something to learn 🙂

    Thanks
    Lijin

  9. A very useful post indeed! Just a note on the first code snippet:

    String path = System.getProperty(“catalina.base”)
    + System.getProperty(“file.seperator”)
    + “YOUR_FILE.properties”;

    The System.getProperty(“file.seperator”) can be replaced with: File.separator (a convenience field in the java.io.File class)

Leave a comment