获取java.rmi.UnmarshalException:无法识别的方法hash:远程对象不支持的方法
我是RMI技术的新手。获取java.rmi.UnmarshalException:无法识别的方法hash:远程对象不支持的方法
当我运行rmi客户端程序时,出现异常:java.rmi.UnmarshalException:无法识别的方法hash:远程对象不支持的方法。 我正在使用jdk1.0
远程方法的参数是序列化对象。
这些服务器代码...
这是远程接口
package interfacepackage;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ServerInterface extends Remote{
public void getOrder(Order order) throws RemoteException;
}
这是服务器实现类
public class ServerImplementation implements ServerInterface {
public ServerImplementation() throws RemoteException {
}
public void getOrderFromCash(Order order) throws RemoteException {
System.out.println("WORKED");
}
public static void main(String[] args)
try {
java.rmi.registry.LocateRegistry.createRegistry(1234);
ServerImplementation service = new ServerImplementation();
ServerInterface myRemoteObject = (ServerInterface) UnicastRemoteObject
.exportObject(service, 0);
java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry
.getRegistry("localhost", 1234);
registry.rebind("ServerImplementation", myRemoteObject);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
这是Order类
public class Order implements Serializable{
private static final long serialVersionUID = 1L;
private int id;
private String name;
public Order(int id,String name){
this.id=id;
this.name=name;
}
}
我在Client中也有相同的Interface和Order类。
这是客户端代码
public class TestClientProgram {
public static void main(String[] args) {
try{
java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.getRegistry("localhost",1234);
ServerInterface service=(ServerInterface) registry.lookup("ServerImplementation");
Order orderObject=new Order(1,"dish");
service.getOrderFromCash(orderObject);
}
catch(Exception e){
e.printStackTrace();
}
}
}
可以在任何一个可以帮助我如何解决这个问题呢?
由于事先 Renjith中号
的异常指示该服务器不能找到方法,该方法是由客户端(错误消息被稍微误导)调用。一种可能的原因可能是服务器和客户端正在运行不同的类路径,并且代码已被修改为足以使RMI接口不兼容。
这里有点问题。您的ServerInterface
有一个getOrder
方法,但实施有getOrderFromCash
。我会检查以确保所有的代码都是用相同版本的接口编译和执行的。
感谢您的回复。 实际上这是我的错误。客户代码是这样的:
public class TestClientProgram {
public static void main(String[] args) {
try{
java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.getRegistry("localhost",1234);
ServerInterface service=(ServerInterface) registry.lookup("ServerImplementation");
Order orderObject=new Order(1,"dish");
service.getOrder(orderObject);
}
catch(Exception e){
e.printStackTrace();
}
}
}
我已经解决了这个问题。
我已经将classpath环境变量设置为Order类的根目录。
现在我可以从本地主机调用远程方法。
但是我试图从不同的计算机执行远程方法时遇到了其他异常。
一台计算机作为服务器运行(例如:服务器IP为192.168.1.108)。 我的客户端代码正好是这样的:
public class TestClientProgram {
public static void main(String[] args) {
try{
java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.getRegistry("192.168.1.108", 1234); //
ServerInterface service = (ServerInterface) registry.lookup("ServerImplementation");
Order orderObject=new Order(12,"DISH");
service.getOrder(orderObject);
}
catch(NotBoundException e){
System.out.println("NotBoundException");
e.printStackTrace();
}
catch(RemoteException e){
System.out.println("RemoteException");
e.printStackTrace();
}
}
}
我得到的例外是:
java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is:
java.net.ConnectException: Connection refused
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
我不知道,为什么这个错误发生。我能够ping通两台计算机,并检查服务器中的端口,并且它正在工作。 这两台电脑在Ubuntu中运行。 但我的困惑是为什么上面的例外指向IP 127.0.1.1,即使我连接到IP 192.168.1.108(服务器的IP)。
但是我试图从不同的计算机执行远程方法时遇到了其他异常。 一台计算机作为服务器运行(例如:服务器IP为192.168.1.108)。我的客户代码完全是这样的:
即使这是一个旧帖子,如果有人再次遇到问题,我可能会有解决方案。您必须通过检查文件/ etc/hosts来检查服务器是否具有正确的IP。带有计算机名称的条目必须具有您为服务器设置的IP。
这是一个迟到的答案,但它可以帮助新用户。
我正在使用RMI(客户端和服务器) 我得到了同样的错误,一切都正确,但我错过了在服务器界面中定义函数,而在客户端界面中定义它!
我希望这个答案能帮助你:)
你是否开始rmiregistry? – 2011-09-29 19:27:03