Java - RMI客户端“NoSuchMethod”问题

时间:2016-06-03 12:38:53

标签: java server client rmi

所以我试着编写一个RMI服务器/客户端。服务器工作得很好。但是客户端给了我这个错误:

Exception in thread "main" java.lang.NoSuchMethodError: stub class initialization failed
    at Addition_Stub.<clinit>(Unknown Source)
    at sun.misc.Unsafe.ensureClassInitialized(Native Method)
    at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
    at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:140)
    at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1057)
    at java.lang.reflect.Field.getFieldAccessor(Field.java:1038)
    at java.lang.reflect.Field.getLong(Field.java:591)
    at java.io.ObjectStreamClass.getDeclaredSUID(ObjectStreamClass.java:1663)
    at java.io.ObjectStreamClass.access$700(ObjectStreamClass.java:72)
    at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:480)
    at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:468)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:468)
    at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:365)
    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:602)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1622)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at java.rmi.Naming.lookup(Naming.java:101)
    at AdditionClient.main(AdditionClient.java:8)

客户代码:

import java.rmi.*;

public class AdditionClient {
    public static void main (String[] args) {
        AdditionInterface hello;
        try {
            System.setSecurityManager(new RMISecurityManager());
            hello = (AdditionInterface)Naming.lookup("rmi://localhost/ABC");
            int result=hello.Add(9,10);
            System.out.println("Result is :"+result);

            }catch (Exception e) {
                System.out.println("HelloClient exception: " + e);
                }
        }
}

我正在使用以下VM参数运行服务器:

-Djava.security.policy=file:${workspace_loc}/RMIClientSide/security.policy -Djava.rmi.server.codebase=file:${workspace_loc}/RMIServerSide/bin/

我的security.policy包含以下内容:

grant {
        permission java.security.AllPermission;
};

添加界面如下所示:

   import java.rmi.*;

   public interface AdditionInterface extends Remote {
       public int Add(int a,int b) throws RemoteException;
   }

-------------服务器端-----------:

AdditionServer:

import java.rmi.*;
import java.rmi.server.*;   

public class AdditionServer {
       public static void main (String[] argv) {
           try {
               System.setSecurityManager(new RMISecurityManager());

               Addition Hello = new Addition();                    
               Naming.rebind("rmi://localhost/ABC", Hello);

               System.out.println("Addition Server is ready.");
               }catch (Exception e) {
                   System.out.println("Addition Server failed: " + e);
                }
           }
}

AdditionInterface:

import java.rmi.*;

   public interface AdditionInterface extends Remote {
       public int add(int a,int b) throws RemoteException;

   }

增加:

import java.rmi.*;
import java.rmi.server.*;

public class Addition extends UnicastRemoteObject
         implements AdditionInterface {

      public Addition () throws RemoteException {   }

      public int add(int a, int b) throws RemoteException {
          int result=a+b;
          return result;
      }
 }

0 个答案:

没有答案