title: Howto deserialize class, if class version was changed
body: <p>I want to write the code, that can deserialize the class even if the class was changed (but you have old class version). </p>
  
  <p>The idea is simple. </p>
  
  <ol>
  <li>Read the <code>serialVersionUID</code> of the serialized class</li>
  <li>Check if that <code>serialVersionUID</code> is equal to the <code>serialVersionUID</code> of the current class version.</li>
  <li>If not, create new <code>ClassLoader</code> and load the old class version into workspace.</li>
  </ol>
  
  <p>I thought to use something like this:</p>
  
  <pre><code>FileInputStream file = new FileInputStream(filename);
  ObjectInputStream o = new ObjectInputStream(file) {
      protected Class&lt;?&gt; resolveClass(java.io.ObjectStreamClass desc) throws IOException, ClassNotFoundException {
          //5639490496474438904L is the suid of the older version
          if (desc.getSerialVersionUID() == 5639490496474438904L) {
              Class c01 = null;
              URL eineURL = new URL("file://localhost/U:/MyJavaProj/bin/test/oldversion/");
              URL[] reiURL = new URL[] {eineURL};
              URLClassLoader clazLader = new URLClassLoader(reiURL);
              c01 = clazLader.loadClass("test.SerObj");
              return c01; 
          }
          return super.resolveClass(desc);
      }
  };
  
  SerObj obj = (SerObj) o.readObject();
  </code></pre>
  
  <p>The problem is in the last line. My current class version is placed in <strong>U:/MyJavaProj/bin/test/SerObj.class</strong>
  My old class version is placed in <strong>U:/MyJavaProj/bin/test/oldversion/test/SerObj.class</strong></p>
  
  <p>In the last line I read the old class version but cast it to the new version :(</p>
  
  <p>Has anyone some idea or maby any other aproach to add the versioning support for the serialization in Java?</p>