title: Sending an object through a socket in java
body: <p>I have 2 java netbeans projects, one is the Server other is the Client. I have a Message class that I have created which I want to pass to the server and back the other way to the client after modification done at the server. I have included the class Message in both projects. I use <code>ObjectOutputStream</code> and <code>ObjectInputStream</code> to pass the object. The connection between the server and the client is ok and the object passes through but at the server when I read the object from the <code>ObjectInputStream</code> using <code>readObject()</code> method I type cast it to the <code>Message</code> class. But an <em>ClassNotFoundException</em> is thrown at the server. It cant find the Message class. How can I resolve this?</p>
  
  <p>Code for the client:</p>
  
  <pre><code>public void startClient() {
      try {
          // Create the socket
          Socket clientSocket = new Socket(HOST, PORT);
          // Create the input &amp; output streams to the server
          ObjectOutputStream outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
          ObjectInputStream inFromServer = new ObjectInputStream(clientSocket.getInputStream());
  
          // Read modify
          // TODO here
  
          /* Create The Message Object to send */
          LinkedList&lt;Message&gt; msgList = new LinkedList&lt;&gt;();
          Message msg = new Message();
          msg.setMessage("Kasun");
          msg.setIndex(1);
          msg.setAverage(5.5f);
          msgList.push(msg);
  
          /* Send the Message Object to the server */
          outToServer.writeObject(msgList);            
  
          /* Retrive the Message Object from server */
          LinkedList&lt;Message&gt; inFromServerList = new LinkedList&lt;&gt;();
          Message msgFrmServer = null;
          inFromServerList = (LinkedList&lt;Message&gt;)inFromServer.readObject();
          msgFrmServer = inFromServerList.pop();
  
          /* Print out the recived Message */
          System.out.println("Message: " + msgFrmServer.getMessage());
          System.out.println("Index: " + msgFrmServer.getIndex());
          System.out.println("Average: " + msgFrmServer.getAverage());
  
  
          clientSocket.close();
  
      } catch (Exception e) {
          System.err.println("Client Error: " + e.getMessage());
          System.err.println("Localized: " + e.getLocalizedMessage());
          System.err.println("Stack Trace: " + e.getStackTrace());
      }
  }
  </code></pre>
  
  <p>Code for the Server</p>
  
  <pre><code>public void startServer() {
  
      try {
          ServerSocket welcomeSocket = new ServerSocket(PORT);
  
          while (true) {    
              // Create the Client Socket
              Socket clientSocket = welcomeSocket.accept();
              System.out.println("Socket Extablished...");
              // Create input and output streams to client
              ObjectOutputStream outToClient = new ObjectOutputStream(clientSocket.getOutputStream());
              ObjectInputStream inFromClient = new ObjectInputStream(clientSocket.getInputStream());
  
              // Read modify
              // TODO here
  
              /* Create Message object and retrive information */
              LinkedList&lt;Message&gt; inList = new LinkedList&lt;&gt;();
              Message inMsg = null;
              inList = (LinkedList&lt;Message&gt;)inFromClient.readObject();
              inMsg = inList.pop();
  
              /* Modify the message object */
              inMsg.setMessage(inMsg.getMessage().toUpperCase());
              inMsg.setIndex(5);
              inMsg.setAverage(10.5f);
  
              /* Send the modified Message object back */
              outToClient.writeObject(inMsg);        
  
          }
  
      } catch (Exception e) {
          System.err.println("Server Error: " + e.getMessage());
          System.err.println("Localized: " + e.getLocalizedMessage());
          System.err.println("Stack Trace: " + e.getStackTrace());
          System.err.println("To String: " + e.toString());
      }
  }
  </code></pre>
  
  <p>The exception thrown at the "server"</p>
  
  <pre><code>java.lang.ClassNotFoundException: rusl.online.examination.system.client.utility.Message
  </code></pre>
  
  <p>Would I have to use java RMI? Is there a solution for this without using RMI?</p>