感觉我不理解某种基本的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>
,但这些不起作用,所以我去尝试将序列化数据转换为HashMap
我的类扩展但仍然得到错误。我不知道发生了什么,如果有人能指出这里的问题吗?
这是stacktrace:
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)
答案 0 :(得分:0)
我想我可以从堆栈跟踪中看到问题。序列化发生在
net.mastrgamr.gtfs.static_objects.DataMaps.getMap
但例外是
java.lang.ClassNotFoundException: DataMaps
如果这是相同的DataMaps类,则会读取
java.lang.ClassNotFoundException: net.mastrgamr.gtfs.static_objects.DataMaps
您已将数据序列化到文件中,但随后将数据包添加到DataMaps,这使它们成为不同的类。