Why not just pass the properties file as an argument to your main method? That way you can load the properties as follows:
public static void main(String[] args) throws IOException {
Properties props = new Properties();
props.load(new BufferedReader(new FileReader(args[0])));
System.setProperties(props);
}
The alternative: If you want to get the current directory of your jar file you need to do something nasty like:
CodeSource codeSource = MyClass.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
File jarDir = jarFile.getParentFile();
if (jarDir != null && jarDir.isDirectory()) {
File propFile = new File(jarDir, "myFile.properties");
}
... where MyClass
is a class from within your jar file. It's not something I'd recommend though - What if your app has multiple MyClass
instances on the classpath in different jar files (each jar in a different directory)? i.e. You can never really guarantee that MyClass
was loaded from the jar you think it was.