4

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
  • 424
  • 2
  • 7
  • 17
  • possible duplicate of [java.rmi.ServerException: RemoteException occurred in server thread](http://stackoverflow.com/questions/9531158/java-rmi-serverexception-remoteexception-occurred-in-server-thread) – user207421 May 03 '13 at 10:10

3 Answers3

2

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 !!

akshaykumar6
  • 2,147
  • 4
  • 18
  • 31
  • 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. – user207421 Oct 24 '12 at 22:16
  • 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) – Rod Oct 25 '12 at 00:06
  • Then try adding the policy file with your project, then add the code for granting permission using security manager in server, refer http://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" – akshaykumar6 Oct 25 '12 at 04:10
  • 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. – user207421 Oct 25 '12 at 07:38
  • Actually, what I'm trying to do is to follow this tutorial: http://www.cs.put.poznan.pl/pawelw/javarmi.html#A%20Reference%20Cell%20as%20an%20Object%20Dowloaded 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 Oct 25 '12 at 13:47
  • 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? – Rod Oct 25 '12 at 13:53
  • @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 Oct 26 '12 at 01:01
1

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 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? – Rod Oct 23 '12 at 23:37
  • @Rod Put it into the client's JAR file? – user207421 Oct 24 '12 at 00:15
-2

Solution for NoClassFound Exception when running RMI Client.

Case:

The server and client files are in different folders.

Example:

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

Client side: The server interface and the client are inside Project Folder: C:\RMIClient Package: rmiClient

Problem: Could not find the location of the server interface.

Solution: In the client side, 1. Create a package name rmiServer (the package name should be the same as the server side package). 2. Place the server stub inside this package. 3. The client.java is inside the rmiClient package. Now import the rmiServer package in the client.java file. import rmiServer.*;

This will solve the classNotFound Exception that happens when we execute the client side RMI.

samsad beagum
  • 171
  • 1
  • 2
  • It will solve it if that's the cause; that information had already been stated [here](http://stackoverflow.com/a/13053155/207421) six months ago; and there are quite a few other causes. – user207421 May 03 '13 at 10:11