0

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!

carlspring
  • 31,231
  • 29
  • 115
  • 197
MiH
  • 409
  • 1
  • 4
  • 16

2 Answers2

0

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

Alexander Petrov
  • 9,204
  • 31
  • 70
0

You don't have the stub on the CLASSPATH of the Registry. The easy way round this is to start the Registry in the server JVM, via LocateRegistry.createRegistry().

You may then find you don't have it in the client CLASSPATH either.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • But it still error with : java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: java.net.ConnectException: Connection refused: connect – MiH Jul 21 '16 at 19:16
  • @EJP if you look into my comments I have told him this like 1 hour ago. – Alexander Petrov Jul 21 '16 at 19:27
  • @AlexanderPetrov You're kidding, right? I should read 60 comments? All I read was your incorrect answer. Good to see you've now fixed it to agree with mine. – user207421 Jul 22 '16 at 00:32
  • @ejp it is just the 4th out of 50 comments. Anyway it was nice to see you confirming my thoughts though your answer is still partial. – Alexander Petrov Jul 22 '16 at 06:38
  • @AlexanderPetrov As is yours. There is also the option of starting `rmiregistry` with the `-classpath` option, or from a specific working directory such that the default CLASSPATH of `.` will allow loading of the stub apclasses. I don't consider these to be much practical use, and ditto the codebase feature frankly. – user207421 Jul 22 '16 at 10:11
  • @EJP see my comment number 7 yesterday. all is there, just don't thing putting the whole server in the registry so a good solution. That is why it is not in my final answer. – Alexander Petrov Jul 22 '16 at 10:18
  • @AlexanderPetrov I am really not going to read 60, or 55, or 7, or even 4, comments on what at the time was a wrong answer prior to writing my own. Your expectations are misplaced. – user207421 Jul 22 '16 at 10:36