关于RMI的问题,作业,急求问题原因

michael_galvin 2009-10-30 06:57:39
我在写了一个很简单的测试程序,发现没有办法运行。请大家指点。
代码如下:
import java.rmi.Remote;

public interface SerInterface extends Remote{

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


public class ServerImpl extends UnicastRemoteObject implements SerInterface {

private static final long serialVersionUID = 1L;
public ServerImpl()throws RemoteException{
System.out.println("server is strating ...");
}
public void f(){
System.out.println("a client call");
}
}
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;


public class Server {


public static void main(String[] args) {
System.out.println("a client is strating...");
System.out.println("hello");
try {
LocateRegistry.createRegistry(1099);
ServerImpl si = new ServerImpl();
System.out.println("binging server...");
Naming.rebind("server",si);
System.out.println("waiting for invocations from clients...");
} catch (Exception e) {
e.printStackTrace();
}

}

}


import java.rmi.Naming;


public class Client{

public static void main(String[] args){


try {
ServerImpl s = (ServerImpl)Naming.lookup("server");
s.f();
} catch (Exception e) {
e.printStackTrace();
}

}
}

java.lang.ClassCastException: $Proxy0 cannot be cast to ServerImpl

服务器可以跑起来,客户端是不行的。
请问是什么原因呀?

...全文
117 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangjun88019014 2009-10-31
  • 打赏
  • 举报
回复
你需要使用rmi编译器编译你的实现类
rmic cn.wzh.ServerImpl
将会生成stub存根对象,也就是你的远程代理对象

你在client端main方法应该使用接口调用
SerInterface s = (SerInterface)Naming.lookup("server");

启动Server时,注意启动RMI服务器
开始 -> 运行 -> start rmiregistry
littlemonster 2009-10-31
  • 打赏
  • 举报
回复
龙果兄:
那现在分布式的啥比较流行?
  • 打赏
  • 举报
回复
RMI 现在几乎没人用了,不用刻意地去研究
jl08033 2009-10-30
  • 打赏
  • 举报
回复
LZ你没结过帖??????????
LZ你没结过帖??????????
LZ你没结过帖??????????
littlemonster 2009-10-30
  • 打赏
  • 举报
回复
你定义这远程接口下没写方法呀?接口里的每一个方法必须声明它将产生一个RemoteException异常

应该把你的f()方法写里面抛异常
import java.rmi.Remote;

public interface SerInterface extends Remote{

public void f() throws RomoteException;

}

我觉得现在应该可以了,不过我没法试。。。

67,542

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧