I am using the Maven onejar plugin (https://code.google.com/p/onejar-maven-plugin/) to create an uberjar.
I want to access a properties file which is in the root of my classpath like this:
Properties prop = new Properties();
try {
prop.load(new FileInputStream("Db.properties"));
driver = prop.getProperty("driver");
url = prop.getProperty("url");
username = prop.getProperty("username");
password = prop.getProperty("password");
} catch (IOException ex) {
LOG.debug(ex.toString());
}
conn = null;
My log4j.properties file which is in the same directory is found because i can do logging... What is my problem? :/ But the Db.properties isnt found.
FileInputStream is used to load resources from files located on the file system. Files inside a jar are not on the file system. You need to use a different InputStream.
For this case, using the ClassLoader#getResourceAsStream(String) method would be advised. It returns an InputStream resource found on the classpath. Something like:
InputStream is = getClass().getClassLoader().getResourceAsStream("/Db.properties");
should work. Or for convenience:
InputStream is = getClass().getResourceAsStream("/Db.properties");
Of note, the reason the log4j.properties
works is because Log4j by design can load configuration files in the root classpath.