救命啊,剛開始學習rmi,問題不斷,備受打擊

april_HZX發表於2004-03-27
按照網上的資料:
//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)

高手們幫幫忙啊,試了好幾個,都是這樣

相關文章