Reputation: 43
Is there a way to modify the property values in a Properties file that is stored in a Jar file as a Resource?
This is the scenario that I am trying to handle. I have a properties file stored as a resource in my Jar file. There are some system specific properties, for example, paths. I want to be able to change this for the system that I want the Jar file to run on.
The best solution would be to call a class/method that is internal to the Jar file that will modify the resource file that is contained in the Jar. Another solution would be to run a utility program on the Jar file that will modify the property file and re-Jar everything. This will be a one-time setup process that I run on the Jar file.
Upvotes: 4
Views: 6318
Reputation: 272237
I think you may find the Apache Commons Configuration project of use.
You can set this up to use properties files, and to have the concept of a system and a user properties file. You can then deploy to your system property file, and this is overridden by the user property file. Writing a property will modify the user properties file.
So you deploy your system properties file with your defaults, and changes are maintained in the overriding user properties file. I've used this in the past for exactly what you're doing, and it works very nicely. There's a huge number of ways to configure this library, so it's worth looking at the examples and FAQ.
Upvotes: 1
Reputation: 39606
Use the preferences API to store changes: http://java.sun.com/j2se/1.5.0/docs/api/java/util/prefs/package-frame.html
Edit after re-reading the question: are you in a situation where you want to send out the JAR to potentially many people and have it customized on install? If yes, then prefs is the way to go.
If, instead, you're looking to customize for a relatively small number of deployments, then it's probably better to do the customization at build-time.
Upvotes: 3