我正在研究一个项目(这是IS作业),我们的教授给了我们一个.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
Proffesor的课程代码:
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
答案 0 :(得分:1)
通过将代码复制/粘贴到测试项目中,运行时没有错误...
你的异常可能来自包声明,它应该是你教授给你的Course课程的writecoursefile。