There is a client and server. The client sends the me.nyanguymf.console.net.Packet package to the server via an ObjectOutputStream:

/** Sends request to server. */ public boolean sendRequest(Packet request) { try { out.writeObject(request); out.flush(); return true; } catch (IOException e) { e.printStackTrace(); return false; } } 

The server accepts this object:

 if (super.isInterrupted()) break; Object obj = null; try { try { obj = in.readObject(); } catch (SocketException expected) { System.err.println("Lost connection"); break; } if (!(obj instanceof Packet)) throw new ClassNotFoundException(); Packet packet = (Packet) obj; System.out.println("Got clients packet: " + packet.toString()); } catch (ClassNotFoundException expected) { System.err.println("Got invalid object from server. Is it up to date?"); expected.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } 

But an error ClassNotFoundException comes up:

 Got invalid object from server. Is it up to date? java.lang.ClassNotFoundException: me.nyanguymf.console.net.Packet at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:348) at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:686) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1868) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1751) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2042) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431) at me.nyanguymf.console.server.net.InputThread.run(InputThread.java:38) 

This same class compiled (along with the rest of the necessary) into a separate .jar library and connected it via Maven:

  <dependency> <groupId>me.nyanguymf.console.net</groupId> <artifactId>Packet</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/PacketLib.jar</systemPath> </dependency> 

What could be wrong, why the error comes out?

    1 answer 1

    Problem solved, line deletion helped

     if (!(obj instanceof Packet)) throw new ClassNotFoundException(); 

    But I can not understand: why obj is not a Packet, if it is sent to him and the next line does not result in casting to the Packet type?

    • one
      Possibly obj loaded by another classifier. - AndreySmelik 10:51 pm
    • @AndreySmelik, wow, didn’t know what this could be: D Can you tell me where you can learn more about this topic? - Vasily White
    • Yes, any good article about Klaszoladery read, I myself honestly not very good at it I understand. javarush.ru/groups/posts/… - AndreySmelik