1

I am new to RMI. From the java docs i found that InvalidClassException is thrown when serial version is not matched among local and remote file. But i wrote a dummy class which always throws a InvalidClassException even though statically created serialVersionUID

  package com.sac.serialization;

  import java.io.*;
  import java.util.*;

  class ClassA // non-serializable parent class
  {
      String parentname;

      // absence of no-arg constructor
      ClassA(String name) {
          parentname = name;
      }
  }

  class ClassB extends ClassA implements Serializable {
      private static final long serialVersionUID = 1L;

      ClassB(String name) {
          super(name);
      }
  }

  public class ClassC {
      public static void main(String args[]) {
          ClassB c1 = new ClassB("Sachin");
          try {
              ObjectOutputStream out = new ObjectOutputStream(
                  new FileOutputStream("employee.dat"));
              out.writeObject(c1);
              out.close();

              ObjectInputStream in = new ObjectInputStream(
                  new FileInputStream("employee.dat"));
              ClassA p = (ClassB) in.readObject();
              in.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }

Can some body help me find the root cause?

1
  • Please post the stack trace in your question. It should have been included in the first place. The text of the error message will tell you exactly what is invalid about the class. Commented May 10, 2015 at 21:47

1 Answer 1

2

ClassA must have an accessible constructor wth no arguments. The nearest non-serializable base class of a serializable class must have a constructor with no arguments that is accessible to the derived class. See the Serialization Specification.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.