Java RMI - ClassNotFound exception

11,582

Solution 1

Both client and server should have same package name. I had the same error yesterday, i corrected it after lots of searching. Try it and tell me if any other error comes.

Mark this as answer, if you find so.Thank You !!

Solution 2

CellImpl has not been exported or made available via the client's CLASSPATH. It needs to either (a) extend UnicastRemoteObject or be exported via UnicastRemoteObject.exportObject(); or (b) be Serializable and available on the client's CLASSPATH.

Share:
11,582

Related videos on Youtube

Rod
Author by

Rod

Updated on September 15, 2022

Comments

  • Rod
    Rod over 1 year

    I'm creating a Java RMI program, but I'm getting a ClassNotFoundException. Could you guys help me figure it out? I'm using Eclipse. Somebody suggested me it was a codebase 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();
        }
    
    }
    

    Client:

    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
    
  • Rod
    Rod over 11 years
    Hmm well, what I have here is: *an interface Cell that extends java.io.Serializable *a class CellImpl that implements Cell *an interface Server that extends Remote *a class ServerImpl that implements server and has a method getcell() that returns a CellImpl All these are server-side(interfaces are on Client as well). So, what do I have to do to get this CellImpl on the Client side?
  • user207421
    user207421 over 11 years
    @Rod Put it into the client's JAR file?
  • user207421
    user207421 over 11 years
    To clarify, two identical classes in different packages are different, not the same, and some RMI developers initially make the mistake of copying source code between packages instead of sharing .class files between client and server JAR files. However there's no evidence here that this is the actual problem here.
  • Rod
    Rod over 11 years
    Hmm...I tried this but the error keep coming. But I will keep package names the same from now on. Thanks for the tip! (I mean, I tried setting the package names the same)
  • akshaykumar6
    akshaykumar6 over 11 years
    Then try adding the policy file with your project, then add the code for granting permission using security manager in server, refer fragments.turtlemeat.com/rmi.php#secure .you have to add the compiling code with your project, i did for netbeans, no idea about eclipse "java -Djava.security.manager -Djava.security.policy=policy-file MyClass"
  • user207421
    user207421 over 11 years
    Why? Server permissions have nothing to do with client-side ClassNotFoundExceptions. He doesn't need a SecurityManager, let alone a .policy, file unless he's trying to use the codebase feature, and there's no evidence of that either. The 'tutorial' you cite is non-normative and contains several mistakes. @Rod Ignore this too.
  • Rod
    Rod over 11 years
    Actually, what I'm trying to do is to follow this tutorial: cs.put.poznan.pl/pawelw/… I need to implement a Publish-Subscriber example, so I can apply this to my graduation project(which I'm working at right now). I'm at the Exercise 2 of this tutorial. Any help will be very much appreciated :) Thanks!
  • Rod
    Rod over 11 years
    Oh, and I'm using Eclipse to program and run my code, so I got confused when you guys told me to rearrange JAR files...I never had to work with JARs until now, should I invest some time in learning about it?
  • user207421
    user207421 over 11 years
    @Rod I've given you the answer, why are you still posting here? The client .jar file must contain CellImpl.class and any class it depends on.
  • user207421
    user207421 about 11 years
    It will solve it if that's the cause; that information had already been stated here six months ago; and there are quite a few other causes.