《Thinking in java》一书中P555页上的RMI例子测试没有成功,帮帮忙!!!!!(10分)

  • 主题发起人 主题发起人 kem
  • 开始时间 开始时间
K

kem

Unregistered / Unconfirmed
GUEST, unregistred user!
《Thinking in java》一书中P555页上的RMI例子测试没有成功,书上愿代码如下:
------------------
//远程接口代码如下:
//: PerfectTimeI.java
// The PerfectTime remote interface
package c15.ptime;
import java.rmi.*;
interface PerfectTimeI extends Remote {
long getPerfectTime() throws RemoteException;
} ///:~
-----------------
//服务器端程序代码如下:
//: PerfectTime.java
// The implementation of the PerfectTime
// remote object
package c15.ptime;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.*;
public class PerfectTime
extends UnicastRemoteObject
implements PerfectTimeI {
// Implementation of the interface:
public long getPerfectTime()
throws RemoteException {
return System.currentTimeMillis();
}
// Must implement constructor to throw
// RemoteException:
public PerfectTime() throws RemoteException {
// super();
// Called automatically
}
// Registration for RMI serving:
public static void main(String[] args) {
System.setSecurityManager(
new RMISecurityManager());
try {
PerfectTime pt = new PerfectTime();
Naming.bind(
"//acerkemble:2005/PerfectTime", pt);

//acerkemble是本机的名称,
//这是对Bruce Eckel的原代码唯一改变的地方。即,colossus--->acerkemble
System.out.println("Ready todo
time");
} catch(Exception e) {
e.printStackTrace();
}
}
} ///:~
----------------------
//客户机程序代码如下:
//: DisplayPerfectTime.java
// Uses remote object PerfectTime
package c15.ptime;
import java.rmi.*;
import java.rmi.registry.*;
public class DisplayPerfectTime {
public static void main(String[] args) {
System.setSecurityManager(
new RMISecurityManager());
try {
PerfectTimeI t =
(PerfectTimeI)Naming.lookup(
"//acerkemble:2005/PerfectTime");
//acerkemble是本机的名称,
//这是对Bruce Eckel的原代码唯一改变的地方。即,colossus--->acerkemble
for(int i = 0;
i < 10;
i++)
System.out.println("Perfect time = " +
t.getPerfectTime());
} catch(Exception e) {
e.printStackTrace();
}
}
} ///:~
------------------
测试的环境是Win2000 Professional下本地一台机上测试,步骤如下:
(1) 使用javac编译远程接口类,远程接口实现类(服务器端程序)和客户机程序。
(2) 使用rmic编译器生成实现类的stub和skeleton。为什么PerfectTime_Stub.class和
PerfectTime_Skel.class两个类默认生成在C:/c15/ptime目录下(工程目录并不是这个目录)??请问是不是
要把它们拷打PerfectTime.class类目录下(在另外的工程目录中)???
(3) 启动RMI注册服务程序rmiregistry。
就是在命令行输入:start rmiregistry 2005 回车
(4) 启动服务器端程序。即,java c15.ptime.PerfectTime ,但是命令行报告有异常如下:
C:/>java c15.ptime.PerfectTime
java.security.AccessControlException: access denied (java.net.SocketPermission a
cerkemble resolve)
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:272)
at java.security.AccessController.checkPermission(AccessController.java:
399)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:545)
at java.lang.SecurityManager.checkConnect(SecurityManager.java:1042)
at java.net.InetAddress.getAllByName0(InetAddress.java:559)
at java.net.InetAddress.getAllByName0(InetAddress.java:540)
at java.net.InetAddress.getByName(InetAddress.java:449)
at java.net.Socket.<init>(Socket.java:100)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirect
SocketFactory.java:25)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMaster
SocketFactory.java:120)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:499)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:190
)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:174)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:318)
at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
at java.rmi.Naming.bind(Naming.java:111)
at c15.ptime.PerfectTime.main(PerfectTime.java:29)
(5) 启动客户机程序。 即,java c15.ptime.DisplayPerfectTime 回车,但是命令行报告有异常如下:
C:/>java c15.ptime.DisplayPerfectTime
java.security.AccessControlException: access denied (java.net.SocketPermission a
cerkemble resolve)
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:272)
at java.security.AccessController.checkPermission(AccessController.java:
399)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:545)
at java.lang.SecurityManager.checkConnect(SecurityManager.java:1042)
at java.net.InetAddress.getAllByName0(InetAddress.java:559)
at java.net.InetAddress.getAllByName0(InetAddress.java:540)
at java.net.InetAddress.getByName(InetAddress.java:449)
at java.net.Socket.<init>(Socket.java:100)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirect
SocketFactory.java:25)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMaster
SocketFactory.java:120)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:499)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:190
)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:174)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:318)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:84)
at c15.ptime.DisplayPerfectTime.main(DisplayPerfectTime.java:12)
请问,我的操作流程正确吗??为什么测试没有通过呢而产生异常呢????
一定要帮帮忙阿。
多谢,多谢,多谢!!
 
各位真的不肯帮忙吗?
多谢!
 
是权限问题,要授权的
 
try the RMI demo of jbuilder, it is easy
 
在JDK下调试,
4) 启动服务器端程序,应为:java -Djava.security.policy=java.policy c15.ptime.PerfectTime
5) 启动客户机程序 ,应为:java -Djava.security.policy=java.policy c15.ptime.DisplayPerfectTime
其中,java.policy为安全策略文件,编辑policy文件
grant {
permission java.security.AllPermission "", "";
};
赋予全部权限试试!
 
后退
顶部