com.sun.proxy。$ Proxy0无法转换为

时间:2018-03-29 21:21:18

标签: java eclipse rmi

我有以下RMI连接代码,它返回一个com.sun.proxy。$ Proxy0无法转换为Client.AdditionInterface错误。我创建了两个独立的包Client&服务器并将接口放入其中。这是我的完整代码:

package Serveur;
import java.rmi.*;
import java.rmi.registry.LocateRegistry; 

public class AdditionServer {

public static void main (String[] argv) {
    try {
        LocateRegistry.createRegistry(1099);     
        Addition Hello = new Addition();     
        Naming.rebind("rmi://"+java.net.InetAddress.getLocalHost().getHostName()+"/ABC", Hello);     
        System.out.println("Addition Server is ready.");
    }
    catch (Exception e) {
        System.out.println("Addition Server failed: " + e);
    }
}
}

//////////////////////////////////////

package Serveur;
 import java.rmi.*;
import java.rmi.server.*;


public class Addition extends UnicastRemoteObject implements AdditionInterface {

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

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

@Override
public String aff(int a, int b) throws RemoteException {
    return String.valueOf(add(a, b));

}
}

////////////////////////////

package Client;

import java.rmi.Remote;
import java.rmi.RemoteException;

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

////////////////////////////

package Client;
import java.rmi.*;


public class AdditionClient {
    public static void main (String[] args) throws Exception {      
        AdditionInterface add = (AdditionInterface) Naming.lookup("rmi://"+java.net.InetAddress.getLocalHost().getHostName()+"/ABC");
        System.out.println("Result is :"+add.add(9, 10));
        System.out.println(add.aff(26, 45));
    }
}

请帮忙吗?谢谢。

1 个答案:

答案 0 :(得分:0)

与客户端的Client.AdditionInterface和服务器的Server.AdditionInterface一样?有效的错误是Server.AdditionInterface cannot be cast to Client.AdditionInterface

使用RMI,您必须在客户端和服务器上具有相同的接口类,但显然服务器实现类(AdditionServer)就在服务器上。界面可以位于共享包中(例如common.AdditionInterface)。