0

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.

AsthaUndefined
  • 1,111
  • 1
  • 11
  • 24
  • Forgot to add, 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. – Ashley Dewald Aug 09 '18 at 04:23
  • Is it acceptable for you to start RMI server on your server app insteed of external process? https://docs.oracle.com/javase/7/docs/api/java/rmi/registry/LocateRegistry.html#createRegistry(int) – Antoniossss Aug 09 '18 at 06:27

1 Answers1

0

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.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • Still does not work, even adding them to a package, separating the classes from the source code and running: java -classpath E:/RMI/classes/ -Djava.rmi.server.codebase=file://E:/RMI/classes/ example.hello.Server I still get the same result, an 'UnmarshalException' and 'ClassNotFoundException'. – Ashley Dewald Aug 09 '18 at 15:25
  • -Djava.rmi.server.code:file:// has to be added the rmiregistry when it is launched not to java when launching it as indicated with the instructions, at least with java version 10.0.2. TBH, it would have been helpful I read my scanned through the table of contents of both my RMI books to realize that I should start reading the book that actually goes into the 'basics' instead of the book that skims over 'non-coding' stuff and has references to RMIC. – Ashley Dewald Aug 13 '18 at 15:48