Search code examples
javarmiclassnotfoundexception

JAVA RMI ClassNotFound


I have spent the last couple of days attempting to get a basic JAVA RMI tutorial server to launch.

I started by launching the command-line and entering the following:

rmiregistry
java -classpath E:\Rmi\src\* -Djava.rmi.server.codebase=file:/E:/RMI/Src/Hello** Server
* I have all my *.java, and *.class files under "E:\RMI\SRC"
** I have attempted to use Hello.class, Hello.Java, made 'Hello' into a jar file and
** I have even-attempted to leave it blank

in an attempt to launch my server. I compiled it in java 8.

Exception that I am getting is:

Server exception: 
java.rmi.ServerException: 
RemoteException occurred in server thread; 
nested exception is: 
java.rmi.UnmarshalException: 
error unmarshalling arguments; 
nested exception is: 
java.lang.ClassNotFoundException: 
Hello java.rmi.ServerException: 
RemoteException occurred in server thread; 
nested exception is: 
java.rmi.UnmarshalException: 
error unmarshalling arguments; 
nested excep tion is: 
java.lang.ClassNotFoundException:
Hello at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:391)

Class definitions are as follows:

Hello.java:

import java.rmi.Remote; 
import java.rmi.RemoteException;  

// Creating Remote interface for our application 
public interface Hello extends Remote {  
   void printMsg() throws RemoteException;  
}

ImplExample.java

// Implementing the remote interface 
public class ImplExample implements Hello {
    // Implementing the interface method 
    public void printMsg() {  
        System.out.println("This is an example RMI program");  
    }  
}

Client.java

    import java.rmi.registry.LocateRegistry; 
    import java.rmi.registry.Registry;  
    public class Client {  
    private Client() {}  
    public static void main(String[] args) {  
       try {  
     // Getting the registry 
        Registry registry = LocateRegistry.getRegistry(null); 

     // Looking up the registry for the remote object 
        Hello stub = (Hello) registry.lookup("Hello"); 

     // Calling the remote method using the obtained object 
        stub.printMsg(); 

     // System.out.println("Remote method invoked"); 
     } catch (Exception e) {
        System.err.println("Client exception: " + e.toString()); 
        e.printStackTrace(); 
     }
  } 
}

Server.java

import java.rmi.registry.Registry; 
import java.rmi.registry.LocateRegistry; 
import java.rmi.RemoteException; 
import java.rmi.server.UnicastRemoteObject; 

public class Server extends ImplExample { 
   public Server() {} 
   public static void main(String args[]) { 
      try { 
         // Instantiating the implementation class 
         ImplExample obj = new ImplExample(); 

         // Exporting the object of implementation class  
         // (here we are exporting the remote object to the stub) 
         Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);  

         // Binding the remote object (stub) in the registry 
         Registry registry = LocateRegistry.getRegistry(); 

         registry.bind("Hello", stub);  
         System.err.println("Server ready"); 
      } catch (Exception e) { 
         System.err.println("Server exception: " + e.toString()); 
         e.printStackTrace(); 
      } 
   } 
} 

The server crashes when it get to the 'registry.bind("hello", stub)'. The stub is not null. Again, 'rmiregistry' at this point has already been launched.

I would truly appreciate any help.


Solution

  • from docs

    The URL of a directory in which the classes are organized in package-named sub-directories

    so URL should point to classes root directory, more or less something like this:

    -Djava.rmi.server.codebase=file://E:/Rmi/src

    Try it out.