I’m trying to local a properties file programmatically without having to pass it’s fullpathname on the commandline to my program. I figure if I can locate the path of the main class, I can stick my properties file in the same directory or a sub-directory.
If that won’t work is there some other way I can locate the path of a properties file without passing it in on the commandline.
Thanks
If the file is expected to be located relative to the main class, you can use
Class.getResource()orClass.getResourceAsStream()as follows:These will return a
URLor anInputStream, respectively; there’s an overload forProperties.load()which accepts anInputStream.You can specify paths in subdirectories, etc. relative to the class’ location, or use the
getResource()method on another class to obtain files relative to that location.If you want to calculate a path relative to the current working directory (which is not necessarily the same as the parent of the main class), then you can use the following to obtain that path:
There’s a list of available system properties in the documentation for
System.getProperties().