In my program, a Java8 application library, written on NetBeans, when I go to the dist folder and run Project.jar
using the command:
java -jar Project.jar
I get the following error:
Sorry, unable to find config.properties
Exception in thread "main" java.lang.NullPointerException
It seems it cannot create the input stream necessary to read the properties file.
Here is the class I use to read the properties:
public class PropertyAccess {
public static String getPropertyFromFile(String propertyName){
String ret = "";
//System.out.println(propertyName);
InputStream input = null;
Properties prop = new Properties();
try {
String filename = "config.properties";
input = PropertyAccess.class.getResourceAsStream(filename);
if(input == null) {
System.out.println("Sorry, unable to find " + filename);
}
//input = PropertyAccess.class.getResourceAsStream("/config/config.properties");
//input = new FileInputStream("config/config.properties");
input = PropertyAccess.class.getClassLoader().getResourceAsStream("config/config.properties");
prop.load(input);
ret = prop.getProperty(propertyName);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return ret;
}
}
There is a bad design/implementation where I load the prop file every time I need to read a property from file, this will be corrected before the main release, but this is not what is causing the issue.
Can someone explain how to fix this?
I've tried to add the file to the classpath, but it also doesnt work.