简体   繁体   中英

When creating an RMI application on two different machines Client and Server Where should we Define our Interface Client side or Server side?

I want to write an RMI application using two laptops which adds two numbers? i have made one laptop as my server and another laptop as my client. when we want to define the interface which is extending from Remote interface on which machine should i define this interface the client side or the server side? please help.

i Have made an RMI application using one machine it works fine I have defined the Interface in the same package but when i work on different machines it does not work.

public interface AdditionI extends Remote {
    public int add(int x ,int y) throws RemoteException;
}

public class Server extends UnicastRemoteObject implements AdditionI {

   public Server() throws RemoteException {}

   @Override
   public int add(int x, int y) throws RemoteException {
       return x+y;
   }

   public static void main(String ar [])throws RemoteException {
       try
       {
           Registry reg = LocateRegistry.createRegistry(2177);
           reg.rebind("Add", new Server());
           System.out.println("Server is ready");
       }
       catch(Exception e)
       {
           System.out.println("Error "+ e);
       }
   }
}



public class Client {

    public static void main(String ar[])throws RemoteException {
        try {
            Registry reg = LocateRegistry.getRegistry("localhost",2177);
            AdditionI ad = (AdditionI)reg.lookup("Add");
            System.out.println("REsult:"+(ad.add(10, 5)));
        } catch (Exception e) {
            System.out.println("Error"+e);
        }
    }

}

when i run this code on the same machine it works fine the result of the method add is displayed, but on different machine it displays the following message.

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

Where should we Define our Interface Client side or Server side?

Simple: you need the interface on both sides.

The Client knows that interface, and basically it is the "only thing" it knows: there is some interface that defines the behavior (the methods) that the client code can use.

The Server knows that interface and implements it.

That interface is the basic thing that "links" client and server (conceptually). They both know that there is some interface AdditionI . The client will need it so it

  • first identify a service that supports that interface
  • when such an service is found, the client knows how to invoke the corresponding add method

The server on the other hand uses the interface to register its implementation as a service ... that clients then can call.

Therefore, you basically have three different parts in your source code:

  • common : that contains that AdditionI interface
  • client : the additional code required to identify and later use that addition service
  • server : the additional code to implement and register the service

And note: that exception java.lang.ClassNotFoundException is really basic. It tells you that the JVM running some code did not find some class.

In other words: your classpath setup is somehow broken. Simply research that exception (you can find endless endless documentation about such basic things, see here for example). Most likely, it boils down to: making sure that some .class files are in the classpath ... where you need them. And the first part already tells you where which classes need to go to!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM