views:

265

answers:

4

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

A: 

1.) i dunno how to answer this one. ¿Where you can find them easily? Don't get the real point of the question (don't get me wrong, not your fault, I'm the one who doesn't understand)

2.) Use environment variables in your OS (like PATH, but using unique names, "APPNAME_CONFIG" or something). Then reference them in your code.

Alfabravo
+1  A: 

In dev mode, put them in source dir and they will be copied to your classes folder, you can then access them using classloader.

Example:

URL url =  ClassLoader.getSystemResource("test.properties");
Properties p = new Properties();
p.load(new FileInputStream(new File(url.getFile())));

In Prod mode, you can make them part of your jar.

Teja Kantamneni
+3  A: 
  • 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.

Bozho
Ok, im using netbeans. I still havent resolved this problem. if i include files in a 'config' package, when i build , they get rolled into a .jar file which defeats the point of having config files! accept what you say about spring config - wi dont mind that being compiled, but i need external access to an ordinary config file. it's driving me nuts ...
MalcomTucker
is it necessary for end-users to be able to edit your config? or just you/your teammates. Check my update for that.
Bozho
yes it is necessary - i'm providing some config options for end users, though i'd rather they didnt have to unzip and re-zip the jar, thats why i want the config file in the root directory. i just dont know how i can do it! thanks for your advice by the way
MalcomTucker
well, you could simply remove it from the package and place it outside, after packaging. Then read it in the way I described above.
Bozho
ok, but i cant find anywhere in netbeans to place it so that it both bets copied to the dist folder and is accessible in debug - any suggestions?
MalcomTucker
+1  A: 

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.

JasCav
how do you access your config files in code?
MalcomTucker
@MalcomTucker - Updated my post to answer your question.
JasCav
cheers, can you provide a code snippet for how you are reading in the config files on app relative paths? I presume you're not hardcoding these locations into your project .. thanks
MalcomTucker