Java RMI exception - ClassNotFound

I am creating a Java RMI program, but getting a ClassNotFoundException. Could you guys help me figure it out? I am using Eclipse. Someone suggested to me that this is a code problem, but how does this relate? Here are the codes for my server and client:

Server:

package server; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; import base.Server; import base.RmiStarter; public class ServerImplStarter extends RmiStarter{ public ServerImplStarter() { super(Server.class); } @Override public void doCustomRmiHandling() { try{ Server engine = new ServerImpl(); Server engineStub = (Server)UnicastRemoteObject.exportObject(engine, 0); Registry registry = LocateRegistry.createRegistry( 1099 ); registry = LocateRegistry.getRegistry(); registry.rebind("Server", engineStub); }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args){ new ServerImplStarter(); } } 

Customer:

 package client; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import base.RmiStarter; import base.Server; import base.Cell; public class CellClient extends RmiStarter { public CellClient() { super(Server.class); } @Override public void doCustomRmiHandling() { try{ Registry registry = LocateRegistry.getRegistry(); Server server = (Server)registry.lookup("Server"); Cell c = null; c = server.getcell(); }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args) { new CellClient(); } } 

and the error is this:

 java.rmi.UnmarshalException: error unmarshalling return; nested exception is: java.lang.ClassNotFoundException: server.CellImpl at sun.rmi.server.UnicastRef.invoke(Unknown Source) at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source) at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source) at $Proxy0.getcell(Unknown Source) at client.CellClient.doCustomRmiHandling(CellClient.java:23) at base.RmiStarter.<init>(RmiStarter.java:19) at client.CellClient.<init>(CellClient.java:13) at client.CellClient.main(CellClient.java:31) Caused by: java.lang.ClassNotFoundException: server.CellImpl 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 java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at sun.rmi.server.LoaderHandler.loadClass(Unknown Source) at sun.rmi.server.LoaderHandler.loadClass(Unknown Source) at java.rmi.server.RMIClassLoader$2.loadClass(Unknown Source) at java.rmi.server.RMIClassLoader.loadClass(Unknown Source) at sun.rmi.server.MarshalInputStream.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 sun.rmi.server.UnicastRef.unmarshalValue(Unknown Source) ... 8 more 
+4
source share
3 answers

Both clients and servers must have the same package name. Yesterday I had the same error, I fixed it after many searches. Try it and tell me if another error has occurred.

Mark this as an answer if you find it. Thanks!

+2
source

CellImpl is not exported or provided through the CLASSPATH client. It must either (a) extend UnicastRemoteObject, or be exported through UnicastRemoteObject.exportObject (); or (b) be Serializable and available on the CLASSPATH client.

+1
source

Solution for eliminating NoClassFound when starting the RMI client.

Happening:

Server and client files are in different folders.

Example:

Server side: server interface and server implementation are inside Project folder: C: \ RMIServer Package: rmiserver

Client side: server interface and client are inside Project folder: C: \ RMIClient Package: rmiClient

Problem: Could not find server interface location.

Decision. On the client side, 1. Create the rmiServer package name (the package name must match the package on the server side). 2. Place the server stub inside this package. 3. The .java client is inside the rmiClient package. Now import the rmiServer package into the client.java file. import rmiServer. *;

This will solve the NotFound class exception that will happen when we run RMI on the client side.

-2
source

Source: https://habr.com/ru/post/1441617/


All Articles