I am having two queries.
i) This is my main class.
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; @SuppressWarnings("javadoc") public class Serializer { public static final String FILE_NAME = "Person.ser"; public static void main(final String[] args) { final Person person = new Person("Akash", 18, new Community("Hindu")); try { final File f = new File(FILE_NAME); if (!f.isDirectory() && f.isFile()) { System.out.println("File :: " + FILE_NAME + " has been deleted ? " + f.delete()); } final ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream(FILE_NAME)); System.out.println("Writing object to file"); System.out.println(person); os.writeObject(person); os.close(); // Reading from file final ObjectInputStream oins = new ObjectInputStream( new FileInputStream(FILE_NAME)); System.out.println("Reading From File"); System.out.println(oins.readObject()); } catch (final FileNotFoundException e) { e.printStackTrace(); } catch (final IOException e) { e.printStackTrace(); } catch (final ClassNotFoundException e) { e.printStackTrace(); } } } class Person implements Serializable { String name; int age; transient private Community community; public Person(final String name, final int age, final Community community) { super(); this.name = name; this.age = age; this.community = community; } public String getName() { return name; } public void setName(final String name) { this.name = name; } public int getAge() { return age; } public void setAge(final int age) { this.age = age; } public Community getCommunity() { return community; } public void setCommunity(final Community community) { this.community = community; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", community=" + community + "]"; } private void writeObject(final ObjectOutputStream oos) { try { oos.defaultWriteObject(); oos.writeObject(community.getName()); oos.writeObject(this.getAge()); oos.writeObject(this.getName()); } catch (final IOException e) { e.printStackTrace(); } } private void readObject(final ObjectInputStream ois) { try { ois.defaultReadObject(); community = new Community((String) ois.readObject()); // person = new Person((String) ois.readObject(), // (int) ois.readObject(), new Community( // (String) ois.readObject())); } catch (ClassNotFoundException | IOException e) { e.printStackTrace(); } } } class Community { String name; public Community(final String name) { super(); this.name = name; } public String getName() { return name; } public void setName(final String name) { this.name = name; } @Override public String toString() { return "Community [name=" + name + "]"; } } I am able to generate a Serialized file in Java 7. But when I am trying to read the same file in Java 8. It is unable to deserialize. I am getting following exception.
java.lang.ClassNotFoundException: Person at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:348) at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:628) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1620) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1521) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1781) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373) at Serializer.main(Serializer.java: Is there anything which is missing?
2) I understand that the purpose of SUID is to identify a class uniquely. And It helps in (un)marshalling thing but how (any simple example would be appreciated). Also can we our own algorithm to generate the SUID and how can it be make compatible across Java Version?
Thanks in advance!