Get started with RMI

Here’s a simple example that uses localhost for both client and server. It was tested on Win2000 machine running JDK1.3.1.

The purpose is to create an rmi server that contains a remote method listFiles which the client can call with a path to retrieve the list of files in that directory.

- Create a directory c:rmi.
- Create a directory c:rmifileservice.
- Create the following files:

c:rmifileserviceIFileService.java:

package fileservice;
 
import java.rmi.server.*;
import java.util.*;
import java.rmi.*;
 
public interface IFileService extends Remote
{
   public List listFiles(String path) throws RemoteException;
}

c:rmifileserviceFileService.java:

package fileservice;
 
import java.util.*;
import java.rmi.*;
 
public class FileServiceClient
{
   public static void main(String []args) {
      if (System.getSecurityManager() == null) {
         System.setSecurityManager(new RMISecurityManager());
      }
        
      try {
         IFileService fs = FileServiceFactory.getFileService();
 
         List list = fs.listFiles(".");
         for (int i=0; i<list.size(); i++) {
            System.out.println(list.get(i));
         }
      }
 
      // not required to catch (RuntimeExceptions) but we'll do
      // got give hints in case something goes wrong
      catch(java.security.AccessControlException e) {
         e.printStackTrace();
         System.out.println("nHint: did you specify -Djava.security.policy=...policy?");
      }
 
      // required to catch
      catch(RemoteException e) {
         e.printStackTrace();
      }
      catch(Exception e) {
         e.printStackTrace();
      } 
   }
}
 
class FileServiceFactory
{
   public static IFileService getFileService() throws Exception {
      return (IFileService) Naming.lookup(IFileService.class.getName());
   }
}

c:rmifileserviceFileServiceClient.java:

package fileservice;
  
import java.util.*;
import java.rmi.*;
  
public class FileServiceClient
{
   public static void main(String []args) {
      if (System.getSecurityManager() == null) {
         System.setSecurityManager(new RMISecurityManager());
      }
        
      try {
         IFileService fs = FileServiceFactory.getFileService();
 
         List list = fs.listFiles(".");
         for (int i=0; i<list.size(); i++) {
            System.out.println(list.get(i));
         }
      }
 
      // not required to catch (RuntimeExceptions) but we'll do
      // got give hints in case something goes wrong
      catch(java.security.AccessControlException e) {
         e.printStackTrace();
         System.out.println("nHint: did you specify -Djava.security.policy=...policy?");
      }
 
      // required to catch
      catch(RemoteException e) {
         e.printStackTrace();
      }
      catch(Exception e) {
         e.printStackTrace();
      } 
   }
}
 
class FileServiceFactory
{
   public static IFileService getFileService() throws Exception {
      return (IFileService) Naming.lookup(IFileService.class.getName());
   }
} 

c:rmifileservicepolicy.txt:

grant {
   permission java.security.AllPermission;
};

- Compile all classes:

   c:rmi> javac fileservice*.java

- Generate the stubs with rmic:

   c:rmi> rmic fileservice.FileService

- Run rmiregistry:

   c:rmi> start rmiregistry

- Start the server:

   C:rmi> java -Djava.security.policy=c:rmifileservicepolicy.txt  
             fileservice.FileService

- Start the client:

C:rmi>java -Djava.security.policy=c:rmifileservicepolicy.txt
             fileservice.FileServiceClient

For more information on the internals of RMI, check the Trail: RMI in Sun’s Java Tutorial.