I devoloped an application and made an jar file. To configure the app there exists an config file. I placed the config file in the directory "conf". The jar file lives beside the conf directory. So I have the following files.
The application should always find the config file in the directory "config" which is in the same directory as jar. To get the config file I use such code.
File config = new File("config/config.xml");
This work because the jar is in the same directory as the conf directory. But I want to write a shell script to run the app. When I start the shell script that could be located anywhere. So the current working directory is the directory where the script lives. And "config/config.xml" couldn't be located from there. Is there a way to write code that always searches for the config path in the same directory as the jar lives?
I suggest that you put the config file elsewhere. I would put the master version of that in the jar file and give a possibility to override values using a user-defined config file. Therefore: 1) The config file won't be necessary in some cases. 2) It will be possible that different users will have different configs. 3) The jar file can be on a read-only filesystem, and generally it honours the idea of separation of the program and data.
How to do it? * Many java programs just use environment variable, like ANT_HOME, CATALINA_HOME. You can have a similar one for your classes. * It can be a well-known hard-coded location, like /etc/, but this system is outlived in java apps I believe. * The best think might be: Use yourjar_HOME and define it in your start-up script. The start-up script will be probably generated during installation.
Best regards, Petr
Get a better web browser:<br /><a href="http://www.mozilla.org/products/firefox/switch.html" target="_blank" rel="nofollow">http://www.mozilla.org/products/firefox/switch.html</a>
subject: How to detect a config file when using a shell script to start the app