救命啊,刚开始学习rmi,问题不断,备受打击

04-03-27 april_HZX

按照网上的资料:

//PerfectTimeI.java

package c15.rmi;

import java.rmi.*;

interface PerfectTimeI extends Remote{

long getPerfectTime() throws RemoteException;

}

//PerfectTime

package c15.rmi;

import java.rmi.*;

import java.rmi.server.*;

import java.rmi.registry.*;

import java.net.*;

public class PerfectTime extends UnicastRemoteObject implements PerfectTimeI

{

public long getPerfectTime() throws RemoteException

{

return System.currentTimeMillis();

}

public PerfectTime() throws RemoteException

{

}

public static void main(String[] args) throws Exception

{

System.setSecurityManager( new RMISecurityManager());

PerfectTime pt=new PerfectTime();

Naming.bind("//localhost:2005/PerfectTime",pt);

System.out.println("Ready to do Time");

}

}

//DisplayPerfectTime

package c15.rmi;

import java.rmi.*;

import java.rmi.registry.*;

public class DisplayPerfectTime

{

public static void main(String[] args) throws Exception

{

System.setSecurityManager( new RMISecurityManager());

PerfectTimeI t=(PerfectTimeI)Naming.lookup("//localhost:2005/PerfectTime");

for(int i=0;i<10;i++)

System.out.println("Perfect time= "+t.getPerfectTime());

}

}

编译后把PerfectTime和PerfectTimeI放在c:\java\rmi\server下面

PerfectTime和DisplayPerfectTime放在c:\java\rmi\client下面

然后用rmic产生PerfectTime_Stub.class和PerfectTime_Skel.class

然后在policy文件里面写

grant {

permission java.security.AllPermission "","";

};

保存为policy.txt放在c:\java\rmi\下面

然后 start rmiregistry 2005

然后 c:\java\RMI\server>java -Djava.rmi.server.codebase=file:///c:/java/rmi/server/ -Djava.security.policy=file:///c:/java/rmi/policy c15.rmi.PerfectTime

错误:

Exception in thread "main" java.security.AccessControlException: access denied (

java.net.SocketPermission 127.0.0.1:2005 connect,resolve)

at java.security.AccessControlContext.checkPermission(AccessControlConte

xt.java:270)

at java.security.AccessController.checkPermission(AccessController.java:

401)

at java.lang.SecurityManager.checkPermission(SecurityManager.java:542)

at java.lang.SecurityManager.checkConnect(SecurityManager.java:1044)

at java.net.Socket.connect(Socket.java:420)

at java.net.Socket.connect(Socket.java:376)

at java.net.Socket.<init>(Socket.java:291)

at java.net.Socket.<init>(Socket.java:119)

at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirect

SocketFactory.java:22)

at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMaster

SocketFactory.java:128)

at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:562)

at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185

)

at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)

at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:313)

at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)

高手们帮帮忙啊,试了好几个,都是这样

TheOne2004
2004-03-28 16:32

你那个policy.txt的扩展名不对,policy.all