-1

Storing java objects in memory in a Java jar project. It's not a Springboot project. Just a library that is added to another SpringBoot Project as a dependency.

Can I save an object in memory with in the Jar project ? And how is it achieved? Any suggestions would be appreciated.

CodeLearn
  • 17
  • 1
  • 6
  • You want to store data in jar or in memory as in storing it as runtime data which gets deleted once program is stopped? – deepakchethan Jun 14 '21 at 03:10

1 Answers1

0

Short answer: You should not store objects into your application's JAR file at runtime. Store them in a file in the file system.

Longer answer:

A JAR file on the classpath is liable to be locked. Or read-only. Or in some circumstances, it could be a transient copy in a download cache directory.

Even if you do manage to write to the JAR, it is unspecified what will happen; e.g. whether or not the objects will be visible until the application JVM is restarted.

So given all of these uncertainties, it is not practical to write data into an application's JAR file(s) at runtime.

Another thing is that people (users, system administrators, IT security staff) assume that JAR files are not modified, and are liable to be alarmed and/or annoyed if an application does this.

Finally, there is no real need to write things to a JAR file at runtime. Anything you need to save could / should be written to the file system in ordinary files. There may be a conventional place to put the files, depending on the platform and the nature of the data, or you can make your own convention ... or get the user to say where to put the data via the application's configuration file or command option.


If I haven't convinced you that you that it is a bad idea, see: How can my Java program store files inside of its .jar file?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216