感觉好像我不了解某种基本的Java原理,但是由于某种原因,我无法在自己内部反序列化我的类。当我在主类中运行此命令时,编译器捕获到ClassNotFoundException
。这是我的课:
public class DataMaps<T> extends HashMap<String, T>{
private T t;
public void serialize() {
try {
FileOutputStream fos = new FileOutputStream("stopMap.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this);
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public HashMap getMap() {
//De-serialize map stored in memory
HashMap map = null;
try
{
FileInputStream fis = new FileInputStream("stopMap.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
map = (HashMap)ois.readObject(); //CLASSNOTFOUNDEXCEPTION
ois.close();
fis.close();
} catch(IOException e) {
e.printStackTrace();
} catch(ClassNotFoundException e) {
e.printStackTrace();
}
return map;
}
}
public class Main {
private Stops stop;
private DataMaps<Stops> stopsDataMap;
private String[] stopInfo;
public Main() {
stopsDataMap = new DataMaps<Stops>();
try {
BufferedReader br = new BufferedReader(new FileReader("stops.txt"));
String line = br.readLine();
while((line = br.readLine()) != null){
stopInfo = line.split(",");
stop = new Stops(stopInfo[0], stopInfo[2], stopInfo[4], stopInfo[5]);
stopsDataMap.put(stop.getStopId(), new Stops(stopInfo[2], stopInfo[4], stopInfo[5]));
System.out.println(stop);
}
//stopsDataMap.serialize(); //commented out since the file exists
System.out.println(stopsDataMap.getMap().toString()); //Produces the error
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
System.out.println("Hello");
new Main();
}
}
最初,我将序列化的映射强制转换为我创建的类和
DataMaps
,但是它们不起作用,所以我去尝试将序列化的数据转换为扩展的DataMaps<T>
类,但仍然收到错误。我不知道这是怎么回事,如果有人可以在这里指出问题?这是堆栈跟踪:
java.lang.ClassNotFoundException: DataMaps
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.readOrdinaryObject(ObjectInputStream.java:1769)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at net.mastrgamr.gtfs.static_objects.DataMaps.getMap(DataMaps.java:40)
at net.mastrgamr.gtfs.Main.<init>(Main.java:33)
at net.mastrgamr.gtfs.Main.main(Main.java:43)
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:134)
最佳答案
我想我可以从堆栈跟踪中看到问题。序列化发生在
net.mastrgamr.gtfs.static_objects.DataMaps.getMap
但是例外是
java.lang.ClassNotFoundException: DataMaps
如果这是相同的DataMaps类,它将读取
java.lang.ClassNotFoundException: net.mastrgamr.gtfs.static_objects.DataMaps
您已经将数据序列化到文件中,然后向DataMaps添加了一个包,这使它们实际上成为不同的类。