0

I saw various posts on this topic but they didnt help me. I deleted my Netbeans cache and Compile on Save was not checked. I just try to load a .dat File and use the data that was serialized in there. It works perfectly in JavaEditor but I get the error java.lang.ClassNotFoundException: Example in Netbeans.

The code I used to load the .dat :

  public static void load(){
    ArrayList<Example> ex = new ArrayList<Example>();

    FileInputStream fis;
    ObjectInputStream ois;
    try{
      fis = new FileInputStream("ExampleList.dat");
      ois = new ObjectInputStream (fis);
      while (fis.available()>0){
        Example exam = (Example)ois.readObject();
        ex.add(exam);
      }
      ois.close();
      fis.close();

    } catch (Exception e){
      System.out.println(e);
    }   

The Example.jar:

package ExampleTest;
import java.io.*;
public class Example implements Serializable{
 private String name;

public Example(){
}

//Getter and Setter

}

Doing something in the main method like Example ex = new Example(); works perfectly fine so netbeans should have found the class..

Nais_One
  • 389
  • 6
  • 18

1 Answers1

0

Its a class version compatibility issue, and that happens because you make change on your class after serialized object that you are trying to deserialized,so the new generated serialVersionUID is not equal to previous one,

so you have to declare static long serialVersionUID, by doing that you can safely make changes on your class.

also check that stackoverflow answer

hope that helps.

Ali Alkhatib
  • 83
  • 12
  • the only change I made is to add it to a package.. I didn't even add something like an attribute. Is it possible to just change this and continue using this .dat as before or do i actually have to create a new one? – Nais_One May 23 '17 at 13:56
  • check that java doc http://docs.oracle.com/javase/6/docs/platform/serialization/spec/version.html#6678 – Ali Alkhatib May 24 '17 at 05:45
  • section 5.6.1 Incompatible Changes: point 2 :"Moving classes up or down the hierarchy - This cannot be allowed since the data in the stream appears in the wrong sequence." – Ali Alkhatib May 24 '17 at 05:46