3
votes
public static Reservation[] openBinaryFile( String fileName )
{
    Reservation [] objArray = null;

    try
    { 
        ObjectInputStream inStream = new ObjectInputStream(
            new FileInputStream( fileName ) );

        objArray = (Reservation[])inStream.readObject();

        inStream.close();
    }
    catch( ClassNotFoundException e )
    {
        System.out.println( "Exception: ClassNotFoundException." );
    }

I have the class included, but why do I get an exception? The class is in the same package as the others. Why am I getting this exception?

EDIT: Here is the stack trace run: java.lang.ClassNotFoundException: Reservation at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:247) at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:604) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1575) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496) at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1624) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1323) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) at myViewer.DatabaseFile.openBinaryFile(DatabaseFile.java:42) at myViewer.Viewer.(Viewer.java:175) at myViewer.Viewer.main(Viewer.java:188)

3
Your catch clause discards all the crucial information, namely WHICH CLASS was not found, and which statement caused the exception. Try printing the stack trace instead and post the result.Jim Garrison
Something isn't right about this example. If the JVM didn't know what Reservation was, it wouldn't get past the array declaration.danben
You're not currently printing out the contents of the exception, which means you don't really know what class isn't being found. Maybe it's something the serialized file depends on. In general, when you catch an exception and log it you should almost always print out the exception message.Jon Skeet
@Jon Skeet: I am not sure, but in theory a missing dependency would rather have given a ClassDefNotFoundError or the compilation would just have failed, because the same class is already imported.BalusC
They are apparently not of the same version. Recompile and copy exactly the same class file at both sides and/or include a serialVersionUID as per java.io.Serializable javadoc in the class file at the both sides.BalusC

3 Answers

1
votes

Two things:

  • Make sure that you're actually reading Reservation[]
    • If you're reading a subtype of Reservation (e.g. VIPReservation) then you need the binary for that type
  • Make sure it's the same exact version of Reservation

Immediate actionable:

  • Diagnose the stack trace.

Looking at the stack trace, it expects Reservation to be located in the default package. Make sure that this is the case.

0
votes

The class file in question is not of the same version as the class file from which the instance was originally serialized. Align it out or give them both the same serialVersionUID.

0
votes

What exactly is giving you the exception? An e.printStackTrace() will tell you. Maybe you didn't import java.io.ObjectInputStream?