首先,我正在使用的对象(Book)应该没问题:
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String publisher;
private int numbOfPages;
private int id;
服务器和客户端都有它(复制粘贴,没有别的修改)。所以这绝对不是它。
服务器:
ArrayList<Book> library = this.sql_db.getAllBooks();
try {
ObjectOutputStream objOut = new ObjectOutputStream(
this.client.getOutputStream());
objOut.writeObject(library);
objOut.flush();
}
客户端:
ArrayList<Book> library = new ArrayList<Book>();
try {
ObjectInputStream objIn = new ObjectInputStream(Client.socket.getInputStream());
//library = (ArrayList<Book>) objIn.readObject(); // This is where the stack trace is pointing to
Object o = objIn.readObject(); // well actually here, since I commented last line and added this.
}
同样,书籍在两个程序中是相同的,并且在同一个程序包中。
堆栈跟踪指向我评论的文本,但为了以防万一,这里是:
java.lang.ClassNotFoundException: db.Book
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 java.util.ArrayList.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at client.Client.requestAllBooks(Client.java:39)
at handler.HandleUserRequest.<init>(HandleUserRequest.java:19)
at run.Main.main(Main.java:8)
答案 0 :(得分:0)
该类必须出现在接收者的CLASSPATH上。序列化不适合你。