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

Advertisements

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

  1. Sabari

    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 ๐Ÿ™‚

    Reply
  2. Sarah

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

    Reply
  3. Art Jolin

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

    Reply
  4. Art Jolin

    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.

    Reply
  5. Art Jolin

    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.

    Reply
  6. Raghu

    Hi Prakash,

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

    Reply
  7. prakash

    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.

    Reply
  8. Lijin Post author

    Hi Lemnik,

    You know friend I always look for your comments. I know there will be something to learn ๐Ÿ™‚

    Thanks
    Lijin

    Reply
  9. Jason

    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)

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s