关于rmi的问题???怪怪的.

myxd 2002-09-10 02:29:05
如果我在我的包含*.class的目录中启动rmiregistry,然后再启动的我的ProductServer不会出错。但是在其他目录启动rmiregistry后,在再包含*.class的目录中启动ProductServer出错。


Constructing server implements...
Binding server implements to registry...
Error:java.rmi.ServerException: RemoteException occurred in server thread; neste
d exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested excep
tion is:
java.lang.ClassNotFoundException: ProductImpl_Stub

我的程序如下:

import java.rmi.*;
public interface Product extends Remote
{
public String getDescription() throws RemoteException;
}
import java.rmi.*;
import java.rmi.server.*;

public class ProductClient
{
public static void main(String args[])
{
System.setSecurityManager(new RMISecurityManager());
String url="rmi://localhost/";
try
{
Product c1=(Product)Naming.lookup(url+"toaster");
Product c2=(Product)Naming.lookup(url+"microwave");
System.out.println(c1.getDescription());
System.out.println(c2.getDescription());
}
catch(Exception e){System.out.println("Error:"+e);}
System.exit(0);
}
}
import java.rmi.*;
import java.rmi.server.*;

public class ProductImpl extends UnicastRemoteObject implements Product
{
public ProductImpl(String n) throws RemoteException
{
name=n;
}
public String getDescription() throws RemoteException
{
return "I am a "+name+". Buy me!";
}

private String name;
}
import java.rmi.*;
import java.rmi.server.*;
import sun.applet.*;

public class ProductServer
{
public static void main(String args[])
{
try
{
System.out.println("Constructing server implements...");

ProductImpl p1=new ProductImpl("Blackwell Toaster");
ProductImpl p2=new ProductImpl("ZapXpress Microwave Oven");
System.out.println("Binding server implements to registry...");
Naming.rebind("toaster",p1);
Naming.rebind("microwave",p2);
System.out.println("Waiting for invocations from clients...");
}catch(Exception e){System.out.println("Error:"+e);}
}
}

...全文
30 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
great_sailor 2002-09-12
  • 打赏
  • 举报
回复
说错了一句话, codebase 是告诉rmiregistry 到哪里找到 stub.
great_sailor 2002-09-12
  • 打赏
  • 举报
回复
对不起,来晚了。
我疏忽了安全性的问题,默认的安全管理器SecurityManager不允许网络传输类, 只有自己设定安全管理起和策略。 grant {permission java.security.AllPermission;};

另外策略不一定非要放在server 目录下。

在运行的时候使用如下字符串,
java -Djava.rmi.server.codebase=file:///d:/temp/rmi/ -Djava.security.policy=file:///d:/temp/policy remoteServer
codebase 是告诉rmiregistry 到哪里找到 remoteServer

好了,说了这么多,都是马后炮。
myxd 2002-09-11
  • 打赏
  • 举报
回复
太高兴了。解决了。在server目录下作了一个POLICY就搞定了。太感谢各位兄弟,大哥了。。结分
myxd 2002-09-11
  • 打赏
  • 举报
回复
great_sailor在线吗???
还有其他人原意帮我吗???
myxd 2002-09-11
  • 打赏
  • 举报
回复
great_sailor
谢谢你的回复,这里还得麻烦你了。能不能给我演示一下具体的步骤???
great_sailor 2002-09-10
  • 打赏
  • 举报
回复
在rebind()的时候,rmiregistry 要寻找stub 和 skeleton类来序列化和反序列化客户端的请求。

你的问题比较明白,stub类所在的路径没有被包含进 CLASSPATH 中,但是当前路径 “.”在CLASSPATH中。所以rmiregistry在你的.class路径中启动就没错,但是在其他地方不行。

另外,rmiregistry 不能在包括 stub 的目录下运行,否则客户端不能得到 stub. stub 是要传递给客户的,否则客户没有帮法序列化对server的请求。但是rmiregistry发现当前路径下有stub,就会认为客户已经得到了stub,于是就不传递了。

rmi的深入理解有些复杂的地方,多看看书,在做实验吧!
ezyw 2002-09-10
  • 打赏
  • 举报
回复
呵这个我知道,我研究了好长时间了,你可以在sun的rmi的faq中找到答案:
其原因可能如下:
1.没有指定codebase或指定不正确
2.没有配置webserver提供下载stub,将你的stub放到你的webserver可访问的目录下,再在codebase中指定这个位置以供应用程序下载
izyw@21cn.com
DanielYWoo 2002-09-10
  • 打赏
  • 举报
回复
这个程序我也写过,和bdsc说的基本一样
myxd 2002-09-10
  • 打赏
  • 举报
回复
《java 2和新技术 卷II:高级特性》第209页。2)下的警告。
myxd 2002-09-10
  • 打赏
  • 举报
回复
给怎样办呢???我是照着书上述的呀???
Joeblackyang 2002-09-10
  • 打赏
  • 举报
回复
这很显然嘛,否则它怎么找到stub呀。
bdsc 2002-09-10
  • 打赏
  • 举报
回复
如果我在我的包含*.class的目录中启动rmiregistry,然后再启动的我的ProductServer不会出错。但是在其他目录启动rmiregistry后,在再包含*.class的目录中启动ProductServer出错。

rmiregistry所在的jvm(实例)和你的服务器所用的jvm不是一个,所以:

"如果我在我的包含*.class的目录中启动rmiregistry,然后再启动的我的ProductServer不会出错。" 因为,rmiregistry所用的jvm能找到需要的类。

"但是在其他目录启动rmiregistry后,在再包含*.class的目录中启动ProductServer出错。" 因为,虽然你的ProductServer所用的jvm能找到需要的类,但是rmiregistry所用的jvm却找不到需要的类。
myxd 2002-09-10
  • 打赏
  • 举报
回复
为什么没人回答我呢??????????????????????????????
myxd 2002-09-10
  • 打赏
  • 举报
回复
大伙儿,给电提示三,我都看了好几天书了,还搞不定????

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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