“ClassCastException:$ Proxy0无法强制转换”创建简单的RMI应用程序时出错 [英] "ClassCastException: $Proxy0 cannot be cast" error while creating simple RMI application
问题描述
我正在创建我的第一个非常简单的RMI客户端 - 服务器应用程序。
I am creating my first, very simple RMI client-server application.
以下是代码:
接口ICommunication
Interface "ICommunication"
package itu.exercies.RMI.server;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ICommunication extends Remote
{
public String doCommunicate(String name) throws RemoteException;
}
接口实现CommunicationImpl:
Interface implementation "CommunicationImpl":
package itu.exercies.RMI.server;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class CommunicationImpl extends UnicastRemoteObject implements ICommunication {
/**
*
*/
private static final long serialVersionUID = 1L;
public CommunicationImpl() throws RemoteException {
super();
}
@Override
public String doCommunicate(String name) throws RemoteException {
return "Hello this is server , whats up " +name+ " ?!\n";
}
}
这是我的主要课程服务器CommunicationServer:
Here is my main class of the server "CommunicationServer":
package itu.exercies.RMI.server;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
public class CommunicationServer {
/**
* @param args
* @throws RemoteException
* @throws MalformedURLException
*/
public static void main(String[] args) throws RemoteException, MalformedURLException {
CommunicationImpl imp = new CommunicationImpl();
Naming.rebind("CommunicationImpl", imp);
System.out.println("Server started...");
System.out.println("Object successfully registered. It is bound to the name 'CommunicationImpl'.");
}
}
这是我的客户CommunicationClient:
And this is my client "CommunicationClient":
package itu.exercies.RMI.client;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import itu.exercies.RMI.server.CommunicationImpl;
public class CommunicationClient {
public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {
String url = new String("rmi://localhost/CommunicationImpl");
CommunicationImpl comm = (CommunicationImpl)Naming.lookup(url);
String reply = comm.doCommunicate("Wiktor");
System.out.println(reply);
}
}
现在当我尝试运行它时:
Now when I try to run it:
- 我使用终端导航到项目的bin目录
- 我从那里运行rmiregistry
- 我从新的终端窗口运行我的CommunicationServer(并打印出消息,因此它似乎工作)
- 我打开第三个终端窗口,当我尝试运行我的CommunicationClient时会抛出异常。
- I navigate to bin directory of my project using Terminal
- I run rmiregistry from there
- I run my CommunicationServer from new Terminal window (and it prints out the messages so it seems to work)
- I open third terminal window and when i try to run my CommunicationClient it throws an exception.
java itu线程main中的.exercies.RMI.client.CommunicationClientException java.lang.ClassCastException:$ Proxy0无法在itu.exercies.RMI.client.CommunicationClient.main中强制转换为itu.exercies.RMI.server.CommunicationImpl
(CommunicationClient.java:14)
java itu.exercies.RMI.client.CommunicationClientException in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to itu.exercies.RMI.server.CommunicationImpl at itu.exercies.RMI.client.CommunicationClient.main(CommunicationClient.java:14)
到目前为止,我试图通过使用rmic创建CommunicationImpl对象的存根来修复它现在代替'$ Proxy0'错误引用'CommunicationImpl_Stub':
So far I have tried to fix it by creating a stub of 'CommunicationImpl' object using rmic but now instead of '$Proxy0' the error refers to 'CommunicationImpl_Stub':
线程main中的异常java.lang.ClassCastException:itu.exercies.RMI.server.CommunicationImpl_Stub无法强制转换为itu.exercies.RMI.server.CommunicationImpl
at itu.exercies.RMI.client.CommunicationClient.main(CommunicationClient.java:14)
Exception in thread "main" java.lang.ClassCastException: itu.exercies.RMI.server.CommunicationImpl_Stub cannot be cast to itu.exercies.RMI.server.CommunicationImpl at itu.exercies.RMI.client.CommunicationClient.main(CommunicationClient.java:14)
此时我没有想法是寻找错误。任何人都可以给我任何建议吗?
推荐答案
替换
CommunicationImpl comm = (CommunicationImpl) Naming.lookup(url);
with
ICommunication comm = (ICommunication) Naming.lookup(url);
CommunicationImpl
是<的服务器实现code> ICommunication 。客户既不知道也不关心实现,只关心接口。
CommunicationImpl
is the server implementation of the ICommunication
. The client neither knows nor cares about the implementation, only the interface.
这篇关于“ClassCastException:$ Proxy0无法强制转换”创建简单的RMI应用程序时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!