开发者

Config files - where to put them in Java?

开发者 https://www.devze.com 2022-12-19 18:48 出处:网络
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:

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 fil开发者_如何学Ces 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


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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消