下面是个rmi的介绍,有个完整的例子,我试过的可以完成. (不过是全英文)http://www.codenotes.com/cnp?cnp=J2040017

解决方案 »

  1.   

    请问楼上,我每次执行都会有这个异常,你看是怎么会事情
    Exception in thread "main" java.rmi.ServerException: Server RemoteException; nes
    ted exception is:
            java.rmi.UnmarshalException: error unmarshalling arguments; nested excep
    tion is:
            java.lang.ClassNotFoundException: InterestCalculatorServer_Stub
            at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Stream
    RemoteCall.java:247)
            at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:
    223)
            at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:350)
            at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
            at java.rmi.Naming.rebind(Naming.java:159)
            at RMIFinancialsRegistration.main(RMIFinancialsRegistration.java:8)
    Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested ex
    ception is:
            java.lang.ClassNotFoundException: InterestCalculatorServer_Stub
    Caused by: java.lang.ClassNotFoundException: InterestCalculatorServer_Stub
      

  2.   

    兄弟我也遇到同样的问题,rmi看似很简单,但是在代码已经通过的情况下,仍然执行不了!
    这里有我一个例子以及问题。基本情况:程序代码在jdk1.3下编译完全通过,但不能达到显示 指定文件夹中文件名列表
    的结果!(注:本机测试,本机名为 hanker)
    源代码如下:....................
    FileInterface.java
    ....................import java.rmi.Remote;
    import java.rmi.RemoteException;public interface FileInterface extends Remote {
     public String[] FileDir()  throws RemoteException;
    }...................
    FileImpl.java
    ...................import java.io.*;
    import java.rmi.*;
    import java.rmi.server.UnicastRemoteObject;
    import java.lang.String;
    public class FileImpl extends UnicastRemoteObject
      implements FileInterface{
       public FileImpl() throws RemoteException{
       super();
      }
       public  String[] FileDir() throws RemoteException {
               try { File myDir= new File("d:/rmi");
       String[] dir  = myDir.list();
                     return ( dir);  
                    } catch(Exception e){
                                     e.printStackTrace();
    }
                       return(null);                                     
        }
        }..................
    FileServer.java
    ..................import java.io.*;
    import java.rmi.*;
    public class FileServer {
       public static void main(String args[]) {
          if(System.getSecurityManager() == null) {
             System.setSecurityManager(new RMISecurityManager());
          }
          try {
             FileInterface fi = new FileImpl();
             Naming.rebind("//127.0.0.1/FileServer", fi);
          } catch(Exception e) {
             System.out.println("waiting for the client...");
             System.out.println("FileServer: "+e.getMessage());
             e.printStackTrace();
          }
       }
    }
    ....................
    FileClient.java
    ....................import java.io.*;
    import java.rmi.*;
    public class FileServer {
       public static void main(String args[]) {
          if(System.getSecurityManager() == null) {
             System.setSecurityManager(new RMISecurityManager());
          }
          try {
             FileInterface fi = new FileImpl();
             Naming.rebind("//127.0.0.1/FileServer", fi);
          } catch(Exception e) {
             System.out.println("waiting for the client...");
             System.out.println("FileServer: "+e.getMessage());
             
             e.printStackTrace();
          }
       }
    }
    编译:javac 四个.java文件。然后生成stub 和 sketln: rmic FileImpl运行:
    win2000本机测试:1:start rmiregistry
                    2:java FileServer
                    3:另开命令窗口,java FileClient hanker
                    4:结果:
    FileServer exception:FileInterface
    java.rmi.NotBoundException:FileInterface
    at sun.rmi.transport.StreamRemoteCall.excetpionRemoteCall.
    excetpionReceivedFromServer(StreamRemoteCall.java:245)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java220)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:345)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(unknown sourece)
    at java.rmi.Naming.lookup(Naming.java:84)
    at FileClient.main(FileClient.java:14) 你可以尝试一下整个编译运行过程,虽然最终还是抛出异常!我自己感觉代码已经没有问题了
    基本上你可以看到的rmi版本也就是这样的复杂程度了,什么时候我完全正确了,我会再把代码
    发给你的!
    使用 RMI开发一个分布式应用包括如下几个步骤
    1)定义一个远端的接口
    2)实现这个远端的接口
    3)开发一个服务端
    4)开发一个客户端
    5)生成Stubs 和Skeletons,运行RMI注册器,服务端 和客户
     大家共同进步!
      

  3.   

    我也有一个RMI的小例子,也是网上朋友给的,拿出来共享一下:
      建议使用文本编辑器,不借助Jbuilder
    完整过程实例://RMI-简单使用.txt1、接口代码PerfectTimeI.java
    package c15.rmi;
    import java.rmi.*;
    interface PerfectTimeI extends Remote
    {
    long getPerfectTime() throws RemoteException;
    }2、服务代码PerfectTime.java
    package c15.rmi;
    import  java.rmi.*;
    import  java.rmi.server.*;
    import  java.rmi.registry.*;
    import  java.net.*;public class PerfectTime extends UnicastRemoteObject implements PerfectTimeI
    {
    public long getPerfectTime() throws RemoteException
    {
    return System.currentTimeMillis();
    }
    public PerfectTime() throws RemoteException
    {
    }
    public static void main(String[] args) throws Exception
    {
    System.setSecurityManager( new RMISecurityManager());
    PerfectTime pt=new PerfectTime();
    Naming.bind("//localhost:2005/PerfectTime",pt);
    System.out.println("Ready to do Time");
    }
    }3、客户代码DisplayPerfectTime.java
    package c15.rmi;
    import java.rmi.*;
    import java.rmi.registry.*;public class DisplayPerfectTime
    {
    public static void main(String[] args) throws Exception
    {
    System.setSecurityManager( new RMISecurityManager());
    PerfectTimeI t=(PerfectTimeI)Naming.lookup("//localhost:2005/PerfectTime");
    for(int i=0;i<10;i++)
    System.out.println("Perfect time= "+t.getPerfectTime());
    }
    }4、编译
    F:\java\rmi>javac -d server PerfectTimeI.java PerfectTime.java
    F:\java\rmi>javac -d client PerfectTimeI.java DisplayPerfectTime.java
    生成目录及文件:
    ├─client
    │  └─c15
    │      └─rmi
    │              DisplayPerfectTime.class
    │              PerfectTimeI.class
    └─server
        └─c15
            └─rmi
                    PerfectTime.class
                    PerfectTimeI.class5、生成rmi根
    F:\java\rmi>cd server
    F:\java\rmi\server>rmic c15.rmi.PerfectTime
    又生成两个文件:
    F:\JAVA\RMI\SERVER
    └─c15
        └─rmi
                PerfectTime.class
                PerfectTimeI.class
                PerfectTime_Stub.class
                PerfectTime_Skel.class
    6、拷贝文件 PerfectTime_Stub.class、PerfectTime_Skel.class到client\c15\rmi目录           
    7、编辑策略文件policy:
    grant {
    // Allow everything for now
    permission java.security.AllPermission;
    };
    保存在源文件所在目录中即F:\java\rmi\。
    8、启动rmi注册服务器
    F:\java\rmi>start rmiregistry 2005
    9、启动服务类(为了验证rmi功能,最好屏蔽classpath环境变量,即set classpath=)
    F:\java\rmi\server>java -Djava.rmi.server.codebase=file:///f:/java/rmi/server/ 
    -Djava.security.policy=file:///f:/java/rmi/policy c15.rmi.PerfectTime
    Ready to do Time说明:file:///f:/java/rmi/server/部分的server后面的/不能少
    10、运行客户类(新开一个command窗口。为了验证rmi功能,最好屏蔽classpath环境变量,即set classpath=)
    F:\java\rmi\client>java -Djava.security.policy=file:///f:/java/rmi/policy c15.rm
    i.DisplayPerfectTime
    Perfect time= 1049338846285
    Perfect time= 1049338846295
    Perfect time= 1049338846315
    Perfect time= 1049338846325
    Perfect time= 1049338846325
    Perfect time= 1049338846335
    Perfect time= 1049338846335
    Perfect time= 1049338846335
    Perfect time= 1049338846335
    Perfect time= 1049338846345说明:不能少:-Djava.security.policy=file:///f:/java/rmi/policy,否则无法访问
      

  4.   

    已经可以运行了,结果虽然不是很理想!
    加一个安全策略!!!
    grant {
    permission java.security.AllPermission "", "";
    };
    把这个安全策略粘贴到文本里做一个: policy.txt
    然后 这样运行
     c:\start rmiregistry
     c:\java -Djava.security.policy=policy.txt FileServer
     然后,运行 java FileClient hanker 成功!!!
    (记住,hanker是我的机子名,要在你的机子上调试当然代码里的和这里的要
     用你自己的机子名了!当然你的机子d:盘下要有个rmi的文件夹) 结果:[Ljava.lang.String;@2981ca
    由于文件夹内容不同结果也不同。rmi的问题已经解决了,实现了远程方法调用,结果问题是一般问题了,我明天再看一点小错误。呵呵,就能帮你到这里了兄弟!
      

  5.   

    今天晚上有电,问题已经解决了!心情不错,我把终极代码给你,如何调试
    前面已经很清楚了,运行下面的代码可以得到结果。
    注意:1 我用的是win2000,启动注册rmi命令是  start rmiregistry
          2 我的主机名是 hanker,你要把这个名字改为自己的主机名!
          3 你可以看到fileserver.java里的 127.0.0.1是本机默认的ip,要远程测试当然要
     输入它设的ip了 例如:192.168.0.1
          4 一定要运行安全策略,这是成功的关键,前面已经说清楚了如何运行!
          代码:
    import java.rmi.Remote;
    import java.rmi.RemoteException;public interface FileInterface extends Remote {
     public String[] FileDir()  throws RemoteException;
    }import java.io.*;
    import java.rmi.*;
    import java.rmi.server.UnicastRemoteObject;
    import java.lang.String;public class FileImpl extends UnicastRemoteObject
      implements FileInterface{
        private String name;
        public FileImpl(String s) throws RemoteException{
       super();
       name = s;
      }
          public  String[] FileDir() throws RemoteException {      try { 
       
       File myDir= new File("d:/rmi");
       String[] dir  = myDir.list();
       //String x = new String(contents);
              /**if(FileDir!=null)
                     {for(int i=0;i<FileDir.length;i++)
                          System.out.println(FileDir[i]);
      } */
               
               
              
                return ( dir);  
            
               } catch(Exception e){
                                     e.printStackTrace();
    }
               
                 return(null);                                     
       
       }
                            }
       import java.io.*;
    import java.rmi.*;public class FileServer {
       public static void main(String args[]) {
          if(System.getSecurityManager() == null) {
             System.setSecurityManager(new RMISecurityManager());
          }
          try {
             FileImpl fi = new FileImpl("FileInterface");
             Naming.rebind("//127.0.0.1/FileInterface", fi);
          } catch(Exception e) {
             System.out.println("waiting for the client...");
             System.out.println("FileServer: "+e.getMessage());
             
             e.printStackTrace();
          }
       }
    }import java.io.*; 
    import java.rmi.*;public class FileClient{
       public static void main(String args[]) {   
       
          try {
      
    String host = (args.length > 0) ? args[0] : "hanker"; 
      
     
      FileInterface fi = (FileInterface) Naming.lookup("//"+args[0]+"/FileInterface");
             System.out.println("there is the filelist");
     String[] s= fi.FileDir();
     if(s!=null)
              { for(int i=0;i<s.length;i++)
         System.out.println(s[i]);
          }
       
      
              } catch(Exception e) {
             System.err.println("FileServer exception: "+ e.getMessage());
             e.printStackTrace();
          }
       }
    }文件名称我就不写了,自己看前面的。我的资料也很少,导师给我的资料是E文的并且讲的是corba
    仅仅附带了点rmi。网上的资料基本上不能实现,只是可以借鉴一下,具体步骤我也不多说了,网上很多,慢慢理解!同勉
      

  6.   

    看大家这么热情,我想问一下,我是个闷外汉,现在在看谭浩强的c,如果这本书学完的话,我要学习jave  ,要从那本书学起???大家结合自己的学习经验给我说说呀!不要让小弟走弯路呀!谢谢了!~~
      

  7.   

    yanxianghui(reallove) 我用你的代码编译了一下,执行,在执行server的时候就出错了。
    我你看看错误信息。
    C:\rmi>java -Djava.security.policy=policy.txt FileServer
    waiting for the client...   
    FileServer: Server RemoteException; nested exception is:
            java.rmi.UnmarshalException: error unmarshalling arguments; nested excep
    tion is:
            java.lang.ClassNotFoundException: FileImpl_Stub
    java.rmi.ServerException: Server RemoteException; nested exception is:
            java.rmi.UnmarshalException: error unmarshalling arguments; nested excep
    tion is:
            java.lang.ClassNotFoundException: FileImpl_Stub
            at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Stream
    RemoteCall.java:247)
            at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:
    223)
            at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:350)
            at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
            at java.rmi.Naming.rebind(Naming.java:159)
            at FileServer.main(FileServer.java:11)
    Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested ex
    ception is:
            java.lang.ClassNotFoundException: FileImpl_Stub
    Caused by: java.lang.ClassNotFoundException: FileImpl_Stub
    已经可以看见waiting for clientl了,但是怎么异常总是说找不到FileImpl_Stub类呢
    我的classpath是 .;C:\j2sdk1.4.0_02\jre\lib\;C:\j2sdk1.4.0_02\lib\;C:\rmi;
    所有的类文件都在c:\rmi下面