title: how to create rmi in java
body: <p>hi i have searched the internet for a long time trying to find some thing that talk about how to start rmi registry in windows 7 using cmd so if any one know how to do that pleas let me to know how to do it or if any one can provide us a good link for that... thx in advance </p>
  
  <p>ok thanx for all how answered my question when i asked the question i was not fully understand the RMI system or how it work but know i have good idea i will summarized this for provide all with an idea for the RMI system and if i have any mistake please correct me </p>
  
  <p>so </p>
  
  <p>Remote Interface:</p>
  
  <ol>
  <li><p>We need an interface that extends from the Remote class and defined the method that we would like to invoke remotely<br>
  note:
  Remote is a "marker" interface that identifies interfaces whose methods may be invoked from a non-local virtual machine.     </p>
  
  <pre><code>import java.rmi.Remote; 
  import java.rmi.RemoteException; 
  import java.util.Calendar; 
  
  public interface CalendarTask extends Remote { 
  
    Calendar getDate() throws RemoteException; 
  
  } 
  </code></pre></li>
  </ol>
  
  <p>The Remote Object:</p>
  
  <ol>
  <li><p>We need class that create a Remote object's so we crate class object implement the Remote Interface to make the object's that created by this class object remote object's and we link this object's to the RMI System by extends from this class UnicastRemoteObjec so When a class extends from UnicastRemoteObject, it must provide a constructor declaring this constructor calls super(), it activates code in UnicastRemoteObject, which performs the RMI linking  and remote object initialization.</p>
  
  <pre><code>import java.rmi.RemoteException; 
  import java.rmi.server.UnicastRemoteObject; 
  import java.util.Calendar; 
  
   public class CalendarImpl extends UnicastRemoteObject implements CalendarTask { 
  
    private int counter = 1; 
  
     public CalendarImpl() throws RemoteException {} 
  
     public Calendar getDate() throws RemoteException{
  
    System.out.print("Method called on server:"); 
    System.out.println("counter = " + counter++); 
    return Calendar.getInstance(); 
    } 
  } 
  </code></pre></li>
  </ol>
  
  <p>Writing the Server: </p>
  
  <p>3.1 The server's job is to accept requests from a client, perform some service, and then send the results back to the client.</p>
  
  <p>3.2 The server must specify an interface that defines the methods available to clients as a service. we do that above in the first step (Remote Interface)</p>
  
  <p>3.3 The server creates the remote object, registers it under some arbitrary name, then waits for remote requests</p>
  
  <p>3.4 so for register The remote object we use java.rmi.registry.LocateRegistry class allows the RMI registry service (provided as part of the JVM) to be started within the code by calling its createRegistry() method.</p>
  
  <p>3.5 The java.rmi.registry.Registry class provides two methods for binding objects to the registry. </p>
  
  <p>•  Naming.bind("ArbitraryName", remoteObj); 
     throws an Exception if an object is already bound under the "ArbitrayName".</p>
  
  <p>•  Naming.rebind ("ArbitraryName", remoteObj);
     binds the object under the "ArbitraryName" if it does not exist or overwrites the                object that is bound. </p>
  
  <p>3.6 The example on the following acts as a server that creates a CalendarImpl object and makes it available to clients by binding it under a name of "TheCalendar"</p>
  
  <pre><code>  import java.rmi.Naming; 
    import java.rmi.registry.LocateRegistry; 
  
    public class CalendarServer { 
  
    public static void main(String args[]) { 
        System.out.println("Starting server..."); 
        // Start RMI registry service and bind 
        // object to the registry 
        try { 
            LocateRegistry.createRegistry(1099); 
            Naming.rebind("TheCalendar", 
                    new CalendarImpl()); 
        } catch (Exception e) { 
            e.printStackTrace(); 
            System.exit(1); 
        } 
        System.out.println("Server ready"); 
     } 
   }
  </code></pre>
  
  <p>Writing the Client:</p>
  
  <p>4.1 An RMI client is a program that accesses the services provided by a remote object</p>
  
  <p>4.2 The java.rmi.registry.LocateRegistry class allows the RMI registry service to be located by a client by its getRegistry() method </p>
  
  <p>4.3 The java.rmi.registry.Registry class provides a lookup() method that takes the "ArbitraryName" the remote object was bound to by the server.</p>
  
  <p>Once the client obtains a reference to a remote object, it invokes methods as if the object were local</p>
  
  <pre><code>import java.rmi.registry.*; 
  import java.util.Calendar; 
  
  public class CalendarClient { 
  
    public static void main(String args[]) { 
        Calendar c = null; 
        CalendarTask remoteObj; 
        String host = "localhost"; 
        if(args.length == 1) 
            host = args[0]; 
        try { 
            Registry r = 
              LocateRegistry.getRegistry(host, 1099);
              Object o = r.lookup("TheCalendar"); 
              remoteObj = (CalendarTask) o; 
              c = remoteObj.getDate(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        System.out.printf("%tc", c); 
      } 
    } 
  </code></pre>