Search code examples
javaclassserializationobjectinputstream

Java ObjectInputStream reading from different Application


I saved an ArrayList<MyClass> as object with an ObjectOutputStream. Now, how can I read that object on my another application project with a ObjectInputStream? I tried and it throws an ClassNotFoundException. I have copied MyClass from project 1 to project 2. Do I have to do anything else?


Solution

  • You should not copy a class and the expect that it looks the same - the Java Compiler / the JVM notices the changes. Use exactly the same class file!!!

    There are several versions to do so:

    • Deliver Project1 with Project2 and set up your CLASSPATH
    • Copy the class file in the same package to Project2
    • attach a Jar to Project2 containing MyClass

    And what you never should forgett: Every Serializable should have a serialVersionUID Eclipse automatically makes a warning - click on it and generate it. If you don't have Eclipse, use serialver from the JDK.
    But don't forget: If your class changed so that old versions won't be interpretable by new versions, change the serialversionUID.