【问题标题】:Java - Class Not Found exceptionJava - 未找到类异常
【发布时间】:2014-03-16 21:42:09
【问题描述】:

我正在做一个项目(这是家庭作业),我们的教授给了我们一个 .ser 文件和一个 Course.java 文件(这是他用来创建 .ser 文件的类)我的问题是当我尝试将 .ser 文件中的数据加载到一个数组中,我得到一个 ClassNotFoundException。

我最终做的是在我的项目中创建一个新类,其名称与我的教授创建的类完全相同,并将他的代码复制/粘贴到该类中。我确实做了一些研究,我的 .class 文件在同一个项目文件夹中,我可以在我的项目中使用 Course 类中的方法。

经过一些猜测和检查工作,我知道 ClassNotFoundException 在第 16 行被踢回,

test = readData();

我的代码:

    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

教授班级代码:

   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

错误堆栈:

  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

【问题讨论】:

    标签: java serialization io


    【解决方案1】:

    通过将您的代码复制/粘贴到测试项目中,我在运行它时不会出错...

    您的异常可能来自包声明,应该是您教授给您的 Course 课程的 writecoursefile。

    【讨论】:

    • 哇,回复太快了!但是你能解释一下你的意思吗(我对java还是很陌生)我最终创建了一个名为 writecoursefile 的新项目来更改包名,在复制和粘贴之后我得到了一个 InvalidClassException ......所以它正在取得进展:D(我可能可以从这里拿来研究那个异常,但我想知道你到底在说什么)
    • 其实你用的是什么IDE?例如,使用 Eclipse 通常会导致您强调这些烦人的错误,而 Bob 是您的叔叔 :-)。但是包声明是一种“文件夹”,您可以在其中声明您的类。如果另一个类调用它,你应该在类名前加上它的包名,或者用它的全名导入正确的类(即:import writecoursefile.Course;)
    • 我实际上使用了两个不同的 IDE 的 NetBeans(这是课程所需的 IDE)和 IntelliJ。我使用一个,当我想进行更改或测试一些我不确定的东西时,我会使用另一个。一定要喜欢双显示器!
    • 我将你的答案标记为正确,我最终删除了我的整个项目(当然,我的代码复制到了 txt 文档:P),当我创建新项目时,我牢记你的谈到包,它都符合/运行了,所以谢谢!
    猜你喜欢
    • 2015-06-23
    • 2021-04-14
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多