I would like to be able to run a jar file java -jar myapp.jar
on different folders and have it load config.properties
based on the executing context.
/myapp/myapp.jar /folder1/config.properties /folder2/java -jar /myapp/myapp.jar <------ loads /folder1/config.properties /some/folder2/config.properties /some/folder2/java -jar /myapp/myapp.jar <------ loads /folder2/config.properties
Once the properties are loaded, I want it to then create some files in the current execution folder.
So:
- How do I tell java to load a properties file based on the current executing context?
- How do I get access the folder that the jar was executing from?
Answer
When you provide a relative path to the constructor of the class File
like new File("config.properties")
, behind the scene, the absolute path built is
System.getProperty("user.dir") / config.properties
with user.dir
that is actually the User working directory
which is also the directory from which you launch your command.