Reading Properties File from outside of a JAR File

Add a comment September 14th, 2007

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…

String path = System.getProperty("catalina.base")

                  + System.getProperty("file.seperator")
                  + "YOUR_FILE.properties";
FileInputStream fis = new FileInputStream(path);
Properties props = new Properties();
props.load(fis);
fis.close();

I am putting some more code examples here which I came across my search. Hope this helps anyone.

1. If the resource file is located in your CLASSPATH, then

   objRes = ResourceBundle.getBundle("/ResFile.Application");

These two are also helpful

2. Reading from another JAR (Source : here)
If your program is in an executable jar file then add the following in the manifest file inside your JAR
class-path: jarname.jar
jarname.jar has “some/path/myconfig.properties” file
To access the properties in junk from your executable jar file in your class, do the following

Locale eng = Locale.ENGLISH;
String myvalue  = java.util.ResourceBundle
    .getBundle("some/path/myconfig",eng).getString("greeting");

3. Verifying the Location (Source: here )  In your code, make sure that you are correctly requesting the resource. If you are requesting a resource from the directory where you are running your jar file from (i.e. the working directory), simply use ResourceBundle.getBundle(“Resource”); or equivalent method.

If you bundle is within a directory, you must specify the directory when getting the Bundle as a dot separated string, i.e.
ResourceBundle.getBundle(“etc.i18n.Resource”); would look for the file <working-directory)/etc/i18n/Resource.properties.

I am also putting some links I came across here : 1, 2, 3

Book Mark it-> del.icio.us | Reddit | Slashdot | Digg | Facebook | Technorati | Google | StumbleUpon | Window Live | Tailrank | Furl | Netscape | Yahoo | BlinkList

Popularity: 52% [?]

Bookmark this on BuzzURLBookmark this on BuzzURL Post to TwitterTweets for this web page Bookmark this on FC2 Bookmark newsing it! Choix it! Add to Google Bookmark Bookmark this on Delicious Digg This

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

  1. September 17th, 2007 at 13:56 | #1

    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)

  2. September 17th, 2007 at 14:08 | #2

    Hi Lemnik,

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

    Thanks
    Lijin

  3. April 15th, 2008 at 13:46 | #3
    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.

  4. April 15th, 2008 at 13:53 | #4

    Thanks Prakash :)

  5. February 10th, 2009 at 04:15 | #5

    Hi Prakash,

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

  6. October 1st, 2009 at 08:18 | #6

    Simple and Easy Tip :)

  7. December 4th, 2009 at 18:07 | #7
    Sudhu

    What is the solution to read from property file when it is located at root of webapp i.e outside WEB-INF eg:abc.war/a.properties ?

  1. |
    July 9th, 2009 at 04:12 | #1

    [...] Click here to Read Full Article [...]

Comments feed