gpt4 book ai didi

java - 自定义类自身产生 ClassNotFoundException

转载 作者:太空宇宙 更新时间:2023-11-04 14:13:27 24 4
gpt4 key购买 nike

感觉好像我不理解某种基本的 Java 原理,但由于某种原因我无法在其内部反序列化我的类。当我在主类中运行它时,编译器捕获 ClassNotFoundException 。这是我的类(class):

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();
}

}

首先,我将序列化 map 转换为我创建的类 DataMapsDataMaps<T>但那些不起作用,所以我尝试将序列化数据转换为 HashMap我的类(class)延长了,但仍然收到错误。我不知道发生了什么事,是否有人可以在这里指出这个问题?

这是堆栈跟踪:

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

但异常(exception)的是

java.lang.ClassNotFoundException: DataMaps

如果这是同一个 DataMaps 类,它将读取

java.lang.ClassNotFoundException: net.mastrgamr.gtfs.static_objects.DataMaps

您已将数据序列化到文件,但随后将包添加到 DataMap,这使它们实际上成为不同的类。

关于java - 自定义类自身产生 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28017218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com