Thursday, March 20, 2008

Reading Properties File from outside of a JAR File

Source


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

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


----------------------------

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)


4 comments:

Akanksha said...

Its very interesting article.Very nice blog about the CLASSPATH of that JAR file Special location” like TOMCAT home.Its really informative can you share some more links related to this information.The blog is very useful and helpful.Thanks for sharing it.

digital signature FAQ

Unknown said...

That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
Python training in bangalore
Python course in pune
Python training in bangalore

Unknown said...

I have been meaning to write something like this on my website and you have given me an idea. Cheers.
AWS Training in pune

AWS Online Training

AWS Training in Bangalore

deiva said...

That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
oracle training in chennai

oracle training in omr

oracle dba training in chennai

oracle dba training in omr

ccna training in chennai

ccna training in omr

seo training in chennai

seo training in omr