Friday, 23 August 2013

Error while laoding properties file from jar file

Error while laoding properties file from jar file

I am trying to load a property file from within a jar file but not able do
so. Following is the code of class in which i am loading the file
public class PropertyClass {
private static Properties properties = null;
public String getProperty(String propertyName) {
try {
InputStream inputStream =
this.getClass().getClassLoader().getResourceAsStream("resources/test.properties");
System.out.println("Input Stream " + inputStream);
properties.load(inputStream);
inputStream.close();
if (properties == null) {
System.out.println("Properties null");
}
} catch (IOException e){
e.printStackTrace();
}
return properties.getProperty(propertyName);
}
The class file and the property file both are packed inside the jar. But
when i am trying load the file from another method it gives following
error :-
Input Stream -
sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@9304b1
Exception in thread "main" java.lang.NullPointerException
at PropertyClass.getProperty(PropertyClass.java:16)
It does not show input stream as null Following bit is the line 16 in my
code - properties.load(inputStream);
Any help on this

No comments:

Post a Comment