I have a java app, that needs to save and load user settings. I want to save them in a file located in the JAR file, how could I achieve this?
-
2*I want to save them in a file located in the .jar* Why you want to update jar file on each user pref update ? – jmj Nov 29 '10 at 16:25
-
I want to integrate user specific settings to easily share the current state of my app. It will be a simple textfile, but it shouldnt be somewhere in the filesystem, but in the program itself. – user_unknown Nov 29 '10 at 16:31
4 Answers
That's not possible. Rather consider using java.util.prefs.Preferences
which is designed for exactly this purpose. See also this little guide.

- 1,082,665
- 372
- 3,610
- 3,555
This is not a sensible course of action.
A JAR file is basically just a ZIP file. To rewrite its contents you need to extract them in full, make changes as needed and then write them to a new file that replaces the old one.
If the program that is going to do this is the same one as that contained in the JAR file, this becomes impossible as the file is write protected during execution.
You'd be better advised to store your configuration elsewhere.

- 14,426
- 7
- 55
- 65
That is not the way to store preferences as others said.
If you has to do it that way then :
Locate the JAR from code: How to get the path of a running JAR file?
Unjar the contents to temp folder
Modify in the temp folder
Jar temp folder to the new JAR file.
To add to what Kris said, most security experts will tell you that it's generally a bad security practice to allow end-user applications to modify their own code. What you're asking for would require that.

- 10,990
- 7
- 51
- 80
-
Great but he doesn't want to modify the code, he wants a way to get new data to packaged external files that could be referenced inside the Jar, this is a very handy thing. – Gherbi Hicham Sep 21 '16 at 15:02