我正在努力在java中创建一个简单的RMI服务器。我想通过网络,所以我不能使用本地主机的IP地址。我写了很多代码,但我不断收到一条错误消息:
java.security.AccessControlException: access denied("java.util.PropertyPermission" "java.security.policy" "write")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.System.setProperty(Unknown Source)
at test_Client.TestClient.main(TestClient.java:31)
我已经做了很多寻找,似乎无法找出我的代码有什么问题。我的服务器启动正常,并在我指定的IP和端口号上运行。如果有人能帮助我找出为什么我会收到这个错误的错误。谢谢。 以下是我的客户的示例代码
package test_Client;
//this is the client, it has it's own package
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;
//import all needed rmi files
import test_Interface.Constant;
import test_Interface.testRMIInterface;
//import my constant file so port number can be found.
//import my test interface from my example interface project, example server testing
public class TestClient {
//start my class
public static void main(String[] args) throws RemoteException, NotBoundException{
//throws exceptions so everything is not in try catch blocks...
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
System.setProperty("java.security.policy", "C:/Users/Robert/workspace/ExampleClient");
Registry registryClient = LocateRegistry.getRegistry(Constant.SERVER_IP, Constant.PORT_NUM);
//make my registry client and use port number from constants in example interface
//get the registry on the servers ip address with the port.
testRMIInterface remote = (testRMIInterface) registryClient.lookup(Constant.RMI_ID);
//using testInterface interface under my example interface
//look up the RMI ID and link it to the object created above
//this is the one method i have on the server.
String sendThis = "";
System.out.println("Please enter a word");
Scanner keyboard = new Scanner(System.in);
sendThis = keyboard.next();
System.out.println(remote.returnTest(sendThis));
//simple call to the server method returnTest
//takes in a string. and returns a different one
}
}
答案 0 :(得分:0)
您需要在安装SecurityManager.
否则SecurityManager
会加载默认策略,该策略不授予您更改策略的权限。