Clients find remote services by using a naming or directory service. A naming or directory service is run on a
host and port number that the client is already aware of (for example a well-known port on a public host).

The RMI naming service, a registry, is a remote object that serves as a directory service for clients by keeping a
hash table like mapping of names to other remote objects. It is not necessary to have a single registry on a
particular physical host. An object is free to start its own registry. The behavior of the registry is defined by the
interface java.rmi.registry.Registry. RMI itself includes a simple implementation of this interface
called the RMI Registry. RMI Registry runs on each machine that hosts remote objects and accepts queries for
services, by default on port 1099.

In Simple terms, a remote object is associated with a name in the registry. Any time the client wants to invoke
methods on this remote object it obtains a reference to it by looking up the name. The lookup returns a remote
reference, a stub to the object.

RMI also provides the java.rmi.Naming class that serves as the client’s interaction point with the object
serving as the registry on the host for this lookup. This can be thought of as a client of the RMI Registry.

The naming class’s methods take, as one of their arguments, a name that is a URL-formatted
java.lang.String.
The following diagram shows how the client uses the java.rmi.Naming class to lookup the stub/proxy of the
remote object.

RM RemoteI Object

The program which creates the instance of the remote object also makes use of the java.rmi.Naming class to
bind the remote object to the RMI Registry.

The java.rmi.Naming class
This class behaves as a client to the RMI Registry. It is used on the server side as well as on the client side to
interact with the RMI Registry. On the server side it is used to bind the remote object to the RMI Registry. On
the client side it is used to lookup the remote object.

Methods

All the methods of this class are static.
•  public static void bind (String name, Remote obj)
    It binds the remote object to a string name. The name itself is in the RMI URL format.
  public static Remote lookup (String name)
    It returns a reference, a stub, for the remote object associated with the specified name.
  public static void rebind (String name, Remote obj)
    It rebinds (unbinds the name if it is already bound and binds it again) the specified name if it already in use
    to a new remote object. This could be dangerous if different applications use the same name in the registry
    but is helpful in development.
  public static void unbind (String name)
    It removes the binding with specified name.

Once the client has a stub to the requested object, it can access the remote method transparently, just like local
methods.

The URL takes the form:

rmi://<host_name>[:<name_service_port>]/<service_name>


Where:
host_name is a name recognized on the local area network (LAN) or a DNS name on the Internet.
name_service_port needs to be specified only if the naming service is running on a port other than the default 1099
service_name is the string name that the remote object is associated with in the registry.