I have a java desktop app and the issue of config files is vexing me.
What I want is for my distributable application folder to look like this:
MyApp/Application.jar
MyApp/SpringConfig.xml
MyApp/OtherConfig.xml
MyApp/lib
But at the moment SpringConfig.xml
is inside Application.jar
and I can't even find OtherConfig.xml
programmatically.
I don't care how I set up the various files in my compilation path, so long as they end up looking like the above.
So..
thanks
Most global config files are located in the /etc directory. The following is a list of the most useful of these sub-directories: /etc/X11/ – xorg specific config files. /etc/cups/ – sub-directory containing configuration for the Common UNIX Printing System.
You need to change the configFilePath to the actual location of the file you created above. String configFilePath = "src/config. properties"; FileInputStream propsInput = new FileInputStream(configFilePath);
the spring config file is related to the code and wiring of your application, hence it'd better be inside the jar, and should be subject to change by the users
(new File(".")).getAbsolutePath();
returns the absolute path of your jar - then you can load the OtherConfig.xml
by a simple FileInputStream
if the SpringConfig.xml
contains configuration data like database credentials, put them in an external application.properties
and use a custom PropertyPlaceholderConfigurer
to load the external file.
Answering the question "where do I put the files in my dev setup" is not possible because we don't know your environment.
Actually, if you want to be able to edit the config yourself (and not necessarily end-users), you can open the jar
with any zip software (WinRAR for instance) and edit the config file from within the jar.
Update: as it seems you can't make the config files to be places out of the jar. Well, for a start, you can do it manually - whenever the .jar
is complete, just remove the config file from inside and place it outside.
I typically create a structure where I have a src/ directory and then other directories exist at the same level. Some of those directories include:
At that same level, I then create an Ant script to perform my build so that the appropriate config files, resources, lib, etc are copied into my JAR file upon build. It has worked great for me up to this point and is a fairly easy to understand organizational structure.
Update: Accessing my config files is done, typically, by knowing their location and opening them up and reading them in the code. Because I use Ant to build, I make sure that my config files are in a location that I expect. So, for example, in a recent application I created, when I compile, my JAR file is in the top level directory (relative to the application release structure). Then, there is a "main" config file at that same level. And there is a "theme" config file that is in a themes folder.
To read the various files, I just open them up as I would any other file and read them in and go from there. It's nothing particularly fancy but it works well and it makes it easy to manually change configurations if I need to do so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With