RMI的问题.

做了一个简单的RMI例子,但是一直有问题, 接口:
 
package rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface MyRemote extends Remote {

public String sayHello() throws RemoteException;

}

实现类
 
package rmi;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class MyRemoteImp extends UnicastRemoteObject implements MyRemote {

protected MyRemoteImp() throws RemoteException {

}

public String sayHello() throws RemoteException {
// TODO 自动生成方法存根
return "Server says,'hey'";
}

public static void main(String[] args) {
try {
// 产生远程对象
MyRemote service = new MyRemoteImp();
// 注册服务
Naming.rebind("RemoteHello", service);
System.out.println("++++++++++++++");
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();

}

}

}


客户端类:
 
package rmi;

import java.net.MalformedURLException;
import java.rmi.*;

public class MyRemoteClient {
public static void main(String args[]) {
new MyRemoteClient().go();
}

public void go() {
try {
MyRemote service=(MyRemote)Naming.lookup("rmi:192.168.0.107//RemoteHello");
String s=service.sayHello();
System.out.println(s);

} catch (MalformedURLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (RemoteException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (NotBoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}


在DOS窗口编译成功后执行:
rmic rmi.MyRemoteImp(生成stub文件)
start rmiregistry (打开注册服务)
java rmi.MyRemoteImp(注册)
都顺利执行

打开另一DOS窗口:
执行:java rmi.MyRemoteClient 出现下面错误
Exception in thread "main" java.lang.ClassCastException: sun.rmi.registry.RegistryImpl_Stub
at rmi.MyRemoteClient.go(MyRemoteClient.java:19)
at rmi.MyRemoteClient.main(MyRemoteClient.java:8)

根据提示找到这行:
MyRemote service=(MyRemote)Naming.lookup("rmi:192.168.0.107//RemoteHello");

出现了ClassCastException异常.
Naming.lookup返回的是Remote类.

why????
在网上看了很多贴子,也有人发生一样样的问题,但是没有找到解决办法,大家有没有思路啊?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值