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…

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

4 Responses to “Reading Properties File from outside of a JAR File”


  1. 1 Jason September 17, 2007 at 1:56 pm

    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. 2 Lijin September 17, 2007 at 2:08 pm

    Hi Lemnik,

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

    Thanks
    Lijin

  3. 3 prakash April 15, 2008 at 1:46 pm

    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. 4 Lijin April 15, 2008 at 1:53 pm

    Thanks Prakash :)

Leave a Reply




View Lijin Joseji's profile on LinkedIn

Disclaimer

The information on this site is for informational purposes only. The use of any Trademark or Copyrighted material is not intended to infringe Copyright. This blog is intended to be used under a policy of personal and non commercial use.

Adds

Add to Google

Blog Stats

  • 106,358 hits

Categories