Search This Blog

Wednesday, November 21, 2018

Location of properties file in Java application

props = new Properties();


FileInputStream(..) in Java uses paths relative to the current user directory (see documentation). So it looks for in /home/usr/ or c:\documents and settings\usr. If your .properties file is on the application classpath you can read it throug the Class.getResourceAsStream(..) method.

Do NOT do

FileInputStream inputStream = new FileInputStream(new File(getClass().getResource(url).toURI()));
to load a resource. Because that can work only if the file is loaded from the file system. If you package your app into a jar file, or if you load the classes over a network, it won't work. To get an InputStream, just use

And finally, as the documentation indicates,

will load a file located in the same package as the class Foo.

will load a file located in the package ca.i88.