Is it possible to save the current state of a java program and then reload it? The program is fairly complicated. I just need to be able to save the current state to a file and then reload it. Could you please refer me to a java library or a place to read more about that from.
-
3Not sure if you would be able to fully reload everything one to one, but most programs that do save state, do a dumping of the variable in a file. If the same state needs to be resumed, it just reloads it from the file. This is how "Save state" of Games work. – Shamim Hafiz - MSFT Oct 25 '11 at 11:13
-
See [this thread](http://stackoverflow.com/questions/7777640/what-is-the-best-practice-for-setting-jframe-locations-in-java/7778332#7778332) for a simplistic example (just a few properties) of saving/restoring the state. For a more complex state, it would be worth creating a bean that can be written/read using [`XMLEncoder`](http://download.oracle.com/javase/7/docs/api/java/beans/XMLEncoder.html)/[`XMLDecoder`](http://download.oracle.com/javase/7/docs/api/java/beans/XMLDecoder.html). – Andrew Thompson Oct 25 '11 at 11:34
2 Answers
There is no real way to "store the state of the whole JVM" as such.
But you could encapsulate the relevant state of your application in one or more objects and then serialize those objects. That sounds more complicated than it really is, because most likely the state of your application is already (mostly) encapsulated in some objects.
This serialization tutorial provides more information, for more details see the Java Object Serialization Specification.

- 327
- 2
- 14

- 302,674
- 57
- 556
- 614
The "state" of a java program is a complex beast. This would include the complete heap, the execution point of all threads, values of local variables ...
Most probably you just want to store some state and later load this in a new program. You could use the classes in java.io
to write state to files and later read them again.
http://download.oracle.com/javase/7/docs/api/java/io/package-summary.html

- 14,629
- 5
- 53
- 72