Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java method to save current state of a program

Tags:

java

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.

like image 605
Zee Avatar asked Oct 25 '11 11:10

Zee


People also ask

How do you save an object state in Java?

If your program needs to save state, you can do it the hard way, interrogating each object, then painstakingly writing the value of each instance variable to a file, in a format you create.

How do I save changes in Java?

Make the changes to the contents of the Java file. When you have finished working, click File > Save or File > Save All to save the file and retain all your changes.

How do you save a class object in Java?

Saving object in java can be accomplished in a number of ways, the most prominent of which is to serialize the object - saving it to a file and reading the object using an ObjectOutputStream and ObjectInputStream, respectively. Serialization is a fantastic advantage to using java.

Which of the following is the process of restoring the state of an object?

This process of restoring the object's old state is known as Deserialization.


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.

like image 101
Joachim Sauer Avatar answered Nov 14 '22 23:11

Joachim Sauer


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

like image 33
Arne Deutsch Avatar answered Nov 14 '22 22:11

Arne Deutsch