我只是在学习如何使用 RMI 发送文件的教程。
教程:链接
当我尝试运行我的服务器时遇到错误。我所做的与教程不同的唯一更改是我将端口更改为 1097 而不是默认的 1099。
我该如何解决这个错误?
这是我收到的错误:
C:\Program Files\Java\jdk1.7.0_09\bin\FT_RMI>java -Djava.security.policy=policy.
txt FileServer
FileServer: access denied ("java.net.SocketPermission" "127.0.0.1:1097" "connect
,resolve")
java.security.AccessControlException: access denied ("java.net.SocketPermission"
"127.0.0.1:1097" "connect,resolve")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown S
ource)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown S
ource)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Unknown Source)
at FileServer.main(FileServer.java:11)
第 11 行是 Naming.rebind()。
这里:
FileInterface fi = new FileImpl("FileServer");
Naming.rebind("//127.0.0.1:1097/FileServer", fi);
我尝试将地址更改为 localhost,但出现同样的错误。
解决方案
指向 JRE 文件夹或特定策略文件中的特定 java 策略文件。
java -Djava.security.policy="C:\\FT_RMI\\java.policy" FileServer