I have successfully serialized class Ghost:
class Ghost {}
File file = new File("serialized.class.bin");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(Ghost.class);
Then I’m trying to restore it in a different application:
InputStream is = new FileInputStream(new File("serialized.class.bin"));
ObjectInputStream ois = new ObjectInputStream(is);
Object o = ois.readObject();
And I’m getting error the class is missed while (trying) loading it:
java.lang.ClassNotFoundException: Ghost
I may not access Ghost.class file while deserializing the class. Is it possible to transfer Java classes in a such way?
UPD. I assumed class definition (bytecode) is being dumped while serializing a class. I was wrong. The goal is reachable via getResourceAsStream() although.
You need to have the class file in your class path when deserializing the class, since the deserialized format only contains the values of the class non-transient non-static fields.