Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)
share|improve this question
3  
The message is pretty clear: your deserializing server doesn't have the Person class in its classpath. –  JB Nizet Aug 12 '14 at 20:41
    
Your server's class loader is not aware of the Person class. –  Rafael Winterhalter Aug 12 '14 at 20:41
    
Can someone clarify? It shouldn't have Person though, as these two will be run on different machines. –  Jon Perron Aug 12 '14 at 20:59
    
@JonPerron: How is your server JVM supposed to create an instance of Person, that has been serialized by the client, if it doesn't know the Person class? BTW, how do you intend the list of persons to magically become a list of employees? If you put apples in a box and send the box to a friend, the friend won't find bananas in the box. –  JB Nizet Aug 12 '14 at 21:04
    
It has an Employee class similar to Person. Employee has all of the variables Person has, along with position and salary. That is my reasoning behind casting readObject() to employee and the arraylist. Honestly I thought that'd work :/ –  Jon Perron Aug 12 '14 at 21:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.