Reputation: 228
I am making a super simple server for a board game. It get's a client connection, creates an input stream for a Board Object that I created and sends it back. Whenever I call readObject in the Server or Client I get a java.lang.ClassNotFoundException error. What is causing this?
SERVER SOCKET
import java.net.*;
import java.io.*;
public class GreetingServer extends Thread {
private ServerSocket serverSocket;
public GreetingServer(int port) throws IOException {
serverSocket = new ServerSocket(port);
//serverSocket.setSoTimeout(10000); //Don't timeout
}
public void run() {
while(true) {
try {
PlayerBoard board;
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to " + server.getRemoteSocketAddress());
ObjectInputStream in = new ObjectInputStream(server.getInputStream());
//Get PlayerBoard Object from server.
board = (PlayerBoard)in.readObject(); //LINE THAT GIVES ME EXCEPTION
//Send PlayerBoard back
ObjectOutputStream out = new ObjectOutputStream(server.getOutputStream());
out.writeObject(board);
//Close server
server.close();
}catch(SocketTimeoutException s) {
System.out.println("Socket timed out!");
break;
}catch(IOException e) {
e.printStackTrace();
break;
}
}
}
public static void main(String [] args) {
int port = 25565;
try {
Thread t = new GreetingServer(port);
t.start();
}catch(IOException e) {
e.printStackTrace();
}
}
}
CLIENT SOCKET
import apcslib.*;
import java.net.*;
import java.io.*;
public class GreetingClient {
public static void main(String [] args) {
String serverName = "76.88.3.218";
PlayerBoard board;
int port = 25565;
try {
System.out.println("Connecting to " + serverName + " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to " + client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
ObjectOutputStream out = new ObjectOutputStream(outToServer);
//Send server PlayerBoard object
out.writeObject(new PlayerBoard());
InputStream inFromServer = client.getInputStream();
ObjectInputStream in = new ObjectInputStream(inFromServer);
//Get PlayerBoard object back from server
board = (PlayerBoard)in.readObject(); //LINE THAT GIVES ME EXCEPTION
/*Loop to print out board to console
System.out.println("Board:");
System.out.print(" ");
for(int y = 1; y <= board.getWidth(); y++)
{
System.out.print(y + " ");
}
System.out.println();
for(int y = 0; y < board.getHeight(); y++)
{
System.out.print(Format.left(y+1, 3));
for (int x = 0; x < board.getWidth(); x++)
System.out.print(board.displaySpot(x, y) + " ");
System.out.println();
}*/
//Close connection
client.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
Message I get:
Error:(28, 44) java: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
Upvotes: 0
Views: 2693
Reputation: 681
I have an idea of what's going on, but for reference could you put your PlayerBoard
class up?
For reference as to what I'm thinking: See here .
Here's a general solution to ensure you are able to read the object properly. If the server and client are in different projects, then there are two ways you can achieve a proper result:
PlayerBoard
classes (with the same package names). PlayerBoard
is in a shared jar that you provide to both Server and Client projects. For your demo (this is more likely), if you have both Server and Client in the same project, then you do not need to worry about the above considerations.
All that's left is: PlayerBoard
must implement Serializable
. If you have two PlayerBoard
classes, then both must implement Serializable
in the same way.
Upvotes: 4