Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Config files - where to put them in Java?

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..

  • where do i put the files in my dev setup?
  • and how do i access them programmatically?

thanks

like image 218
MalcomTucker Avatar asked Feb 11 '10 21:02

MalcomTucker


People also ask

Where do I put config files?

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.

How add config file in Java?

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);


2 Answers

  • 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.

like image 111
Bozho Avatar answered Oct 13 '22 16:10

Bozho


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:

  • lib/ - External Libraries
  • config/ - Configuration Files
  • resources/ - Various resources I use (images, etc)

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.

like image 32
JasCav Avatar answered Oct 13 '22 17:10

JasCav