gpt4 book ai didi

java - 当我在 ObjectInputStream 上调用 readObject() 时出现 ClassNotFoundException

转载 作者:行者123 更新时间:2023-12-02 02:50:07 24 4
gpt4 key购买 nike

我正在为棋盘游戏制作一个 super 简单的服务器。它获取客户端连接,为我创建的板对象创建输入流并将其发送回。每当我在服务器或客户端中调用 readObject 时,都会收到 java.lang.ClassNotFoundException 错误。这是什么原因造成的?

服务器套接字

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

客户端套接字

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

我收到的消息:

错误:(28, 44) java: 未报告的异常 java.lang.ClassNotFoundException;必须被捕获或宣布被抛出

最佳答案

我知道发生了什么,但是作为引用,您可以将您的 PlayerBoard 类放上来吗?

有关我的想法的引用:请参阅 here

这是确保您能够正确读取对象的通用解决方案。如果服务器和客户端位于不同的项目中,那么有两种方法可以获得正确的结果:

  1. 您必须有两个相同的 PlayerBoard 类(具有相同的包名称)。
  2. PlayerBoard 位于您提供给服务器和客户端项目的共享 jar 中。

对于您的演示(这种可能性更大),如果您在同一个项目中同时拥有服务器和客户端,那么您无需担心上述注意事项。

剩下的就是:PlayerBoard 必须实现Serializable。如果您有两个 PlayerBoard 类,则两者都必须以相同的方式实现 Serialized

关于java - 当我在 ObjectInputStream 上调用 readObject() 时出现 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43988155/

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