jbuilder中有一个borland的例子。

解决方案 »

  1.   

    ________________________________________________________________________
    RemotePrinter.java
    ^^^^^^^
    package remoteprinter;import java.rmi.*;public interface RemotePrinter extends Remote{
      public void GoPrint() throws RemoteException;
    }________________________________________________________________
    ServerPrinter.java
    ^^^^^^
    package remoteprinter;
    import java.rmi.*;
    import java.rmi.server.*;
    import java.rmi.registry.*;
    import java.util.*;public class ServerPrinter extends UnicastRemoteObject implements RemotePrinter{

    private static int CallTimes=0;

    public ServerPrinter() throws RemoteException{super();}

    public void GoPrint() throws RemoteException{
            CallTimes+=1;
             System.out.println(" Go Print for "+ CallTimes +" times!\n");
       }
      
       public static void main(String[] args) {
         try{
         System.out.println("Prepare to Start Serving! Please Wait...");
         ServerPrinter printer = new ServerPrinter();
         System.out.println("Print Server Ready! Start Serving...");
        
         LocateRegistry.createRegistry();
         Registry registry = LocateRegistry.getRegistry();
         registry.rebind("FirstRemote", printer);
           System.out.println("Server open and ready for customers!!!");
         }
    catch(Exception e){
           System.err.println(e.toString());
         }
       }
    }
    __________________________________________________________________________
    ClientPrinter.java
    ^^^^^^
    package remoteprinter;import java.util.*;
    import java.rmi.*;
    import java.rmi.registry.*;
    import javax.swing.*;public class ClientPrinter{
       public static void main(String[] args){
          try{
            //OLD Implementation
            String url= System.getProperty("ClientPrinter","rmi://localhost/FirstRemote");
            RemotePrinter obj = (RemotePrinter)Naming.lookup(url);
              
            obj.GoPrint();
          }
          catch(Exception e){
            System.err.println(e.toString());
          }
        }
      
    }
      

  2.   

    授人鱼不如授人渔,哈哈。编写Java RMI分布式应用程序的步骤主要包括以下几步:(1) 将远程类的功能定义为Java接口。在Java中,远程对象是实现远程接口的类的实例。在远程接口中声明每个要远程调用的方法。远程接口具有如下特点:1) 远程接口必须声明为public。如果不这样,则除非客户端与远程接口在同一个包内,否则当试图装入实现该远程接口的远程对象时会得到错误结果。2) 远程对象扩展java.rmi.Remote接口。3) 除了所有应用程序特定的例外之外,每个方法还必须抛出java.rmi.RemoteException例外。4) 任何作为参数或返回值传送的远程对象的数据类型必须声明为远程接口类型,而不是实现类。(2) 编写和实现服务器类。该类是实现(1)中定义的远程接口。所以在该类中至少要声明实现一个远程接口,并且必须具有构造方法。在该类中还要实现远程接口中所声明的各个远程方法。(3) 编写使用远程服务的客户机程序。在该类中使用java.rmi.Naming中的lookup()方法获得对远程对象的引用,依据需要调用该引用的远程方法,其调用方式和对本地对象方法的调用相同。
      

  3.   

    写那几个rmi程序很简单,我看大部人在部署上面会花费好长功夫。