How do I implement RMI using the computers connected in the same network?

1.6k views Asked by At

I tried implementing RMI concept using the two computers connected in my company network. I binded the remote object using my ip address and specific port number. Everything worked fine when I tried it in my local machine.

Now In order to access it from the other computer I shared the Adder (extends Remote ) interface .class file with the other computer and a client code. When I tried to access it it throwed the ClassNotFoundException: stub not found.

So I shared the stub.class file which is generated after running the command >> rmic AddImpl. After that it worked fine on the remote computer also.

Now My question is, Is that how RMI is implemented ? Do we need to share both the Adder interface and the stub class file generated in order for a client to access our remote method ?

Below are My classes and interfaces:

interface Adder extends Remote
class AddImpl extends UnicastRemoteObject implements Adder
class Server
class Client
1

There are 1 answers

4
Dickens A S On

You need to share 3 files Adder.class, Adder.java, AdderRemote_Stub.class (not the java code)

Let us assume you have compiled already

  1. create two different directories

  2. launch 3 command prompt

  3. set first 2 command prompt for the first directory (server)

    Example: c:\rmiserver

    put all files here

  4. set last 1 command prompt for the second directory (client)

    Example: c:\rmiclient

    put the client specific files, stub and remote here

  5. (optional) set CLASSPATH=. in all command prompt

    this will allow the current directory files to be recognized in CLASSPATH

  6. important: run rmiregistry command within the command prompt where the server .class files are in CLASSPATH

    run rmiregistry 5000 command in the any of the first 2 command prompt

    keep it running

  7. run your server "java MyServer" in the remaining command prompt of the first 2

  8. run you client "java MyClient" in the last command prompt which is a different directory

    if this is is working then your network logic will work

Duplicate java.rmi.ServerException: RemoteException occurred in server thread (ClassNotFoundException)