• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.ClassNotFoundException Error thrown after changing class structure  RSS feed

 
Kavya Rajan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I had created a project for my assignment at college. I have a class named as TestDetails.java. The structure of the java class is as follows:


studentName:String;
studentRollNumber:String;
examVenue:String;
residentialAddress:String;
telephone:String;

This class was used as a part of another class named as Assessment.java.
I had instantiated this Assessment.java class several times and stored them in the database.

Due to some new specifications given by my prof, I was forced to add a new data - guardianName:String in the TestDetails.java. I had added only one new property. Its alright so long as I create a new object, but if I try to open the old objects using the old structure (without the guardianName:String in TestDetails.java) I am not able to open any of them and the JVM throws a huge error: java.lang.ClassNotFoundException - TestDetails.java


Is there a way to open the old objects although the current class structure is slightly different from the previous one?



Thanks in Advance,
Kavya
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what way did you store them in the database? In a blob column, using Java's serialization mechanism? In that case, you're indeed not going to be able to load those objects again, if you've changed the original class. (This is an example of why Java's serialization mechanism is not very wll suited for long-term storage of data).
 
Kavya Rajan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did store in the database using JSO. How can we overcome this problem? Can you please suggest?


 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you need to do a little switcheroo data conversion. I would try:

1. First revert TestDetails to its old version then create a new class, TestDetails2, or something like that for the new version.
2. Read back all the old TestDetails and copy each of them to a TestDetails2, then saving each TestDetail2 to the database
3. Delete all TestDetails (old version) from the database -- they have all been duplicated as TestDetail2 objects in the database
4. Then, update TestDetails to the new version to match TestDetails2.
5. Then, read all TestDetails2 and copy each to a new TestDetails (the new version) and save TestDetails to the database again.
6. Delete all TestDetails2 so you only have all the converted TestDetails (new version) left.

It may or may not need that many steps. Mind you, I haven't tried this myself but it seems like it would work.

As Jesper said though, the best thing to do is to not serialize Java objects to the database as a blob if there's any chance that the object definition will change at all. Proper use of Object-Relational Mapping (ORM) technologies would have helped you avoid problems like this in the first place.

 
Kavya Rajan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much... I will try your method and also learn more about ORM.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!