I have a java project with the following structure:
And my class that reads said properties
public class PropertyValues {
InputStream inputStream;
HashMap<String,String> mapResources;
public PropertyValues() throws IOException {
try {
Properties prop = new Properties();
String propFileName = "config.properties";
inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
mapResources.put("rname", prop.getProperty("rname"));
mapResources.put("channel", prop.getProperty("channel"));
mapResources.put("serverdb", prop.getProperty("serverdb"));
mapResources.put("portdb", prop.getProperty("portdb"));
mapResources.put("dbname", prop.getProperty("dbname"));
mapResources.put("userdb", prop.getProperty("userdb"));
mapResources.put("passdb", prop.getProperty("passdb"));
mapResources.put("replicationname", prop.getProperty("replicationname"));
mapResources.put("tables", prop.getProperty("tables"));
} catch (Exception e) {
System.out.println("Exception: " + e);
} finally {
inputStream.close();
}
}
I created the "Resources" source folder and inside my config.properties, maybe it doesn't go there, so where? I get a null pointer exception.
Any ideas?
These two links refer to your same problem: https://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream
https://stackoverflow.com/questions/6608795/what-is-the-difference-between-class-getresource-and-classloader-getresource
All the best!