Configure files - where to place them in Java?

I have a java desktop application and the problem with the configuration files annoys me.

I want my application distribution 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 configure the various files in my compilation path, as long as they look like the one shown above.

So..

  • Where can I put files in my dev setup?
  • and how do I access them programmatically?

thanks

+4
source share
4 answers
  • The spring configuration file is associated with the code and wiring of your application, therefore it is better to be inside the bank and can be changed by users

  • (new File(".")).getAbsolutePath(); returns the absolute path of your jar - then you can load OtherConfig.xml simple FileInputStream

  • if SpringConfig.xml contains configuration data, such as database credentials, puts it in an external application.properties and uses a custom PropertyPlaceholderConfigurer to load an external file.

Answering the question "Where can I put files in my dev setup", this is not possible because we do not know your environment.

In fact, if you want to be able to edit the configuration yourself (and not necessarily end users), you can open the jar any zip software (for example, WinRAR) and edit the configuration file from the bank.

Update: apparently you cannot make configuration files in places from the jar. Well, for starters, you can do it manually - whenever the .jar is complete, just delete the configuration file from the inside and place it outside.

+3
source

In dev mode, put them in the source directory and they will be copied to your class folder, after which you can access them using the class loader.

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

+1
source

I usually create a structure where I have the src / directory, and then other directories exist at the same level. Some of these directories include:

  • lib / - External libraries
  • config / - Configuration files
  • resources / - Various resources that I use (images, etc.)

At the same level, I create an Ant script to execute my build so that the corresponding configuration files, resources, lib, etc. were copied to my JAR file during assembly. He did a great job for me until this moment and quite easily understands the organizational structure.

Update . My configuration files are usually accessed by locating them and opening them and reading them in code. Since I use Ant to build, I am sure my configuration files are in the place I expect. So, for example, in a recent application that I created when I compile, my JAR file is in the top-level directory (relative to the release structure of the application). Then at the same level there is a โ€œmainโ€ configuration file. And there is a โ€œthemeโ€ configuration file, which is located in the themes folder.

To read various files, I just open them, like any other file, and read them and go from there. This is nothing special, but it works well, and it makes it easier to manually change the settings if I need it.

+1
source

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

2.) Use environment variables in your OS (for example, PATH, but using unique names, "APPNAME_CONFIG" or something else). Then specify them in your code.

0
source

Source: https://habr.com/ru/post/1301048/


All Articles