Reading serialised object java gives classnotfound exception

95 views Asked by At

Here's my current code:

//export method
public static void exportObj (Object obj, String fname) {
    try {
        // Serialize data object to a file
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fname));
        out.writeObject(obj);
        out.close();
    } catch (IOException e) {}
}
//import method
public static Object importObj (String fname) {
    try {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(fname));
        return in.readObject();
    } catch (IOException e) {}
    return new Object();
}

The export function works fine, I think, it turns my User object into a file and saves it, but then when I try to import it, it gives me a ClassNotFound Exception. What is happening?

1

There are 1 answers

2
Robin Green On BEST ANSWER

All the classes you want to deserialize must exist on the CLASSPATH of the project that contains the import code.