I have a Java project that creates a UI with a table, The table is editable and when you add entries, I have it being saved into a json in a data folder outside of src in the project folder. I'm using Intellij, and I know to create a JAR to make it in artefacts, and the data folder is selected as a resource. But when the JAR is created it cannot find the file data/items.json is this because JARS cannot edit files inside them?
Asked
Active
Viewed 746 times
2
-
Rewrite your code to use file system instead of classpath to operate the configuration files or use some library to persist data in the file system. – CrazyCoder Jun 08 '17 at 09:23
1 Answers
3
Jar files are read only, that means you can't save informations inside the jar. You can save files inside an read them but you can't edit them.
In your case there are more or less two options:
- Save the
*.json
file in e.g the OS Temp path or somewhere else where you can read and write them. - Handle like zip - https://stackoverflow.com/a/4056713/8087490 -
- Locate the JAR file and open as a ZIP archive reader.
- Create a ZIP archive writer to write a new version of JAR file.
- Write the application's files to the writer.
- Write all resources from the ZIP reader to the writer, excluding old versions of the applications files.
- Close the reader and writer.
- Rename the new version of the JAR to replace the old one.

Developer66
- 732
- 6
- 17