Loading a properties file from a path that is not in my class

Hmm, a simple task, but how to load a properties file from a path that is not in my class path?

for example: I have a simple java file that I execute as follows: foo.jar d: /sample/dir/dir/app1.properties and in the code that I do:

public boolean InitConfig(String propePath) { prop = new Properties(); try { InputStream in = this.getClass().getClassLoader().getResourceAsStream(propePath); prop.load(in); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } 

where propePath: d: /sample/dir/dir/app1.properties
and InputStream in is always null. Why is this happening?

+6
source share
1 answer

The only resources that can be loaded using Classloader.getResourceAsStream are the ones in the class (loader) path. To read properties from an arbitrary path, use one of the load functions of the Properties class itself.

 final Properties props = new Properties(); props.load(new FileInputStream(filePath)); 
+16
source

All Articles