0

I'm trying to read the serialized data from a .dat file into its corresponding java class file. My .readObject() method keeps throwing ClassNotFoundException though. Why cant it read the data from the file? I'm trying to read it into a student class who's constructor accepts objects of its type then assigns the values of that object's variables to its variables.

public class StudentTest 
{
    private static ObjectInputStream OIS;
    public static void main(String[] args)
    {

        openFile();
        readFile();
        closeFile();
    }

    public static void openFile()
    {
        try
        {
            OIS=new ObjectInputStream(Files.newInputStream(Paths.get("C:\\Users\\Joey\\Desktop\\students.dat")));
        }
        catch(IOException e)
        {
            System.out.println("Error trying to read file. TERMINATING.");
            System.exit(1);
        }
    }

    public static void readFile()
    {
        try
        {
            while(true)
            {
                Student student=new Student((Student)OIS.readObject());
                System.out.printf("%s", student.toString());
            }
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Class not found. Terminating.");
            System.exit(1);
        }
        catch(EOFException e)
        {
            System.out.println("End of file reached.");
        }
        catch(IOException e)
        {
            System.out.println("Error reading from file. TERMINATING.");
            System.exit(1);
        }
    }

    public static void closeFile()
    {
        try
        {
            if(OIS!=null)
                OIS.close();
        }
        catch(IOException e)
        {
            System.out.println("IOEXCEPTION.");
            System.exit(1);
        }
    }
} 

Here's the stack trace:

java.lang.ClassNotFoundException: files.Student
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at studentfiles.StudentTest.readFile(StudentTest.java:40)
at studentfiles.StudentTest.main(StudentTest.java:16)
8
  • Did you serialize Student into students.dat file using the same Student class, including the package name? Please post your exception stack trace. Commented Sep 17, 2015 at 0:17
  • I downloaded the .dat file from my teacher and I created my own package to hold the Student.java file(That I also downloaded from my teacher) and this file. Commented Sep 17, 2015 at 0:48
  • <terminated>StudentTest [Java Application] <disconnected>studentfiles.StudentTest at localhost:52452 <terminated, exit value: 1>C:\Program Files\Java\jre1.8.0_31\bin\javaw.exe (Sep 16, 2015, 7:40:59 PM) Is this the exception stack trace? Commented Sep 17, 2015 at 0:50
  • 1
    The problem is that Student from serialized file must match the Student compiled into your program, including the package name. It appears that there is a mismatch between what's in serialized file and in your program. Commented Sep 17, 2015 at 0:51
  • Stack trace is something you need to print. Add e.printStackTrace(System.out); before the line that prints "Class not found. Terminating." Commented Sep 17, 2015 at 0:53

1 Answer 1

1

Your Student class is in a different package from what is expected. If you print the exception message that came with the ClassNotFoundException you will see what the expected package was.

And an error creating an ObjectInputStream or FileInputStream is not necessarily an 'error reading from file'. Don't make up your own error messages. Use the one that comes with the exception. Even when you have the right message, as with ClassNotFoundException, you're missing the actual class name that wasn't found, which was in the exception message. Don't do this.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks your right I shouldn't use my own error messages. Especially when I'm just making up nonsense lol
So at the top of the stack trace it says "files.Student". Is this where it thinks my Student class is? In my project I have my Student class in the "studentfiles" package.
Correct, it should be in files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.