Thursday, June 17, 2010

Read a properties file inside jar file

Loading a Properties file into a Jar and running Main class from Jar:
//Issue: I have created my jar file. The main class get some configuration value from a *.properties. This file is contained into jar archive,
but when I run my application, I receive this error: "FileNotFoundException". Here properties file is a member of Jar file only.
//Package structure like:
com/*.java
smpp/test.propeties

Jar File->com/*.class
->smpp/test.properties


//build.xml, for creating jar file


//Java Code, loading properties file in java code
//Main class, main method
InputStream is;
Properties myProp=new Properties();
try {
is = getClass().getResourceAsStream("/smpp/EIF.properties");
myProp.load(is);
}
System.out.println("Size of file :"+is.available());
is.close();

//Here FileInputStream will not work as that is usable only for plain files, not for members inside a jar.
(It works in eclipse, because eclipse loads from the plain files, not from your jar file.)

//Running Jar:
java -jar HelloTest.jar

No comments:

Post a Comment