Hưng Híp Hưng Híp - 4 months ago 25
Java Question

Running RMI Server, got errors with marshalling arguments and class not found

I'm following a tut that show how to create a RMI project.

But when I run the Server site, it has errors. When I run: rmic CalculatorImpl, it just creates stub, I dont see ske.
enter image description here

This is the log when I run rmic CalculatorImpl:

Warning: generation and use of skeletons and static stubs for JRMP is deprecated. Skeletons are unnecessary, and static stubs have been superseded by dynamically generated stubs. Users are encouraged to migrate away from using rmic to generate skeletons and static stubs. See the documentation for java.rmi.server.UnicastRemoteObject.


And when I run CalculatorServer:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: CalculatorImpl_Stub


This is my code:

File CalculatorServer:

public class CalculatorServer {
public CalculatorServer() {
try {
Calculator c = new CalculatorImpl();
Naming.bind("rmi://localhost:1099/CalculatorService", c);
} catch (Exception e) {
System.out.println("Trouble: " + e);
}
}

public static void main(String args[]) {
new CalculatorServer();
}
}


File Calculator:

public interface Calculator extends Remote {
public long add(long a, long b)
throws java.rmi.RemoteException;
}


And file CalculatorImpl:

public class CalculatorImpl extends
java.rmi.server.UnicastRemoteObject implements Calculator {
public CalculatorImpl()
throws java.rmi.RemoteException {
super();
}

public long add(long a, long b)
throws java.rmi.RemoteException {
return a + b;
}
}


And the CalculatorClient in another project:

public class CalculatorClient {

public static void main(String[] args) {
try {
Calculator c = (Calculator)
Naming.lookup(
"rmi://localhost/CalculatorService");
System.out.println( c.add(4, 5) );

}
catch (MalformedURLException murle) {
System.out.println();

System.out.println(
"MalformedURLException");
System.out.println(murle);
}
catch (RemoteException re) {
System.out.println();
System.out.println(
"RemoteException");
System.out.println(re);
}
catch (NotBoundException nbe) {
System.out.println();
System.out.println(
"NotBoundException");
System.out.println(nbe);
}
catch (
java.lang.ArithmeticException
ae) {
System.out.println();
System.out.println(
"java.lang.ArithmeticException");
System.out.println(ae);
}
}
}


So please help me.Tks all!

Answer

Summarizing the comment chain.

What is essential is that the registry should be aware of the Server stub. In order for this to happen, there are two options. Option one is the Registry and the Server to share the same VM. This can be achieved through

adding LocateRegistry.createRegistry() In the beginning of the server main class.

Option two is

two configure the server to use automatic download of the stub files this can be achieved through java.rmi.server.codebase

example command

java -cp c:\home\ann\src;c:\home\ann\public_html\classes\compute.jar
     -Djava.rmi.server.codebase=file:/c:/home/ann/public_html/classes/compute.jar
     -Djava.rmi.server.hostname=mycomputer.example.com
     -Djava.security.policy=server.policy
        engine.ComputeEngine

this is taken from https://docs.oracle.com/javase/tutorial/rmi/running.html

Comments