开发者

Java - ClassNotFoundException I have the class included, but why do I get an exception?

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)


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜