Java - ClassNotFound Exception when working with ObjectInputStream and ArrayList

巧了我就是萌 提交于 2020-01-05 12:10:13

问题


I'm trying to move the contents of an ArrayList from the client to the server. When trying to readObject() the input I receive a ClassNotFoundException. Here is my code, along with the stack trace:

Client:

 ArrayList<Person> people = new ArrayList<Person>();
 ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
 for (Person p : people) {
    output.writeObject(p);
 }

Server:

 ArrayList<Employee> employees = new ArrayList<Employee>();
 ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
 employees = (ArrayList<Employee>) input.readObject(); //Line 47

Error from server:

java.lang.ClassNotFoundException: Person
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at Server.<init>(Server.java:47)
at ServerGUI.main(ServerGUI.java:143)

来源:https://stackoverflow.com/questions/25273658/java-classnotfound-exception-when-working-with-objectinputstream-and-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!