Jeffrey Quinn
Jeffrey Quinn

Reputation: 227

Java - Class Not Found exception

I am working on a project (this IS homework) where our professor gave us a .ser file and a Course.java file (Which is the class that he used to create the .ser file) My issue is that when I try to load the data from the .ser file into an array I get a ClassNotFoundException.

what I ended up doing was creating a new class in my project named exactly the same as the class my professor created and copying/pasting his code into that class. I did do some research and my the .class files are in the same project folder and I am able to use the methods from the Course class in my project.

After some guess and check work I know that the ClassNotFoundException gets kicked back at line 16,

test = readData();

My Code:

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.Serializable;



    public class ObjectSort
     {
public static void main(String[] args) throws Exception
{
     Course[] test = new Course[24];

    test = readData();

} // end main()

// read data from class binary file and put into an array of courses
public static Course[] readData() throws Exception
{
    // create class object to hold data from file
    Course[] holder = null;

    // create file and object input streams
    FileInputStream holderFile = null;
    ObjectInputStream holderObject = null;

    // catch exceptions
    try
    {
        // set up file and stream objects
        holderFile = new FileInputStream("fall2014.ser");
        holderObject = new ObjectInputStream(holderFile);

        // read the object from a file
        holder = (Course[]) holderObject.readObject();

    }
    catch (IOException exc)
    {
        exc.printStackTrace();
    }
    finally
    {
      holderObject.close();
    }

    return holder;
} // end readData()

}// end class ObjectSort

Proffesor's class code:

   import java.io.*;


   class Course implements Serializable {


private String campus;  // the campus on which the course is offered
private String course;  // the course number, such as CSCI 111
private String section; // the section number
private String crn;     // the CRN for this section
private int credits;    // the number od credits for the course
private String time;    // the time the course is offered, such as 8:00 to 10:00 A.M.
private String days;    // the Days the course is offered, suhc as MW


// constructors
Course() {
}

Course(String course, String section, String crn, int credits) {
    this.course = course;
    this.section = section;
    this.crn = crn;
    this.credits = credits;
}   // end Course() initalizing

// muatator methods

public void setCampus(String cmp) {
    this.campus = cmp;
}// end setCampus()

public void setCourse(String crse) {
    this.course = crse;
}// end setCourse()

public void setSection(String sect) {
    this.section = sect;
}   // end setSection()

public void setCRN(String crn) {
    this.crn  = crn;
}   // end setCRN()

public void setCredits(int cr) {
    this.credits = cr;
}   // end setCredits()

public void setTime(String tm) {
    this.time = tm;
}// end setTime()

public void setDays(String days) {
    this.days = days;
}// end setDays()


// accessor methods

public String getCampus() {
    return campus;
}   // end getCampus()

public String getCourse() {
    return course;
}   // end Course()

public String getSection() {
    return section;
}   // end getSection()

public String getCRN() {
    return crn;
}   // end getCRN()

public int getCredits() {
    return credits;
}   // end getCredits()

public String getTime() {
    return time;
}   // end getTime()

public String getDays() {
    return days;
}   // end getDays()


// method to compare by CRN using the String class compareTo()
public int compareTo(Course other) {
    return this.crn.compareTo(other.getCRN());
}   // end compareTO()

// method to return properties as a string
public String toString() {

    return    campus + " "
            + course + " "
            + section + " "
            + crn + " "
            + credits + " "
            + time + " "
            + days;

}    // end toString()

// You will need to add a method to return properties as a CSV string on one line
public String toCSVString()
{
    String record = campus + ","
            + course + ","
            + section + ","
            + crn + ","
            + credits + ","
            + time + ","
            + days + "\n";

    return record;
}  // end toCSVString()

}// end class Course

Error Stack:

  Exception in thread "main" java.lang.ClassNotFoundException: writecoursefile.Course
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:623)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1610)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1515)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1661)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1342)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at ObjectSort.readData(ObjectSort.java:38)
at ObjectSort.main(ObjectSort.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

    Process finished with exit code 1

Upvotes: 1

Views: 1958

Answers (1)

Jean
Jean

Reputation: 682

By copy/pasting your code into a test project, I get no error while running it...

Your exception probably comes from the package declaration, that should be writecoursefile for the Course class that your professor gave you.

Upvotes: 1

Related Questions