线程“main”中的异常 java.security.AccessControlException: access denied (java.util.PropertyPermission * read,write)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10978343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "main" java.security.AccessControlException: access denied (java.util.PropertyPermission * read,write)
提问by bhuvanpavan
I am trying to run a desktop application which is developed in java rmi. While I am trying to execute this application in eclipse, I am getting following error. Please any one help me thanks in advance.
我正在尝试运行在 java rmi 中开发的桌面应用程序。当我尝试在 Eclipse 中执行此应用程序时,出现以下错误。请任何人帮助我提前致谢。
Exception in thread "main" java.security.AccessControlException: access denied (java.util.PropertyPermission * read,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.SecurityManager.checkPropertiesAccess(Unknown Source)
at java.lang.System.getProperties(Unknown Source)
at .HeadOfficeManager.Manager.main(Manager.java:103)
Here is the code.
这是代码。
public static void main(String args[])
{
Manager frame = new Manager();
frame.setVisible(true);
// frame.show(); old 1.4
// Create and install a security manager
if (System.getSecurityManager()== null)
{
System.setSecurityManager(new RMISecurityManager());
}
try
{
Properties prop = System.getProperties();
String httpPath = prop.getProperty("HTTPPath");
new ClassFileServer(80, httpPath);
}
catch (IOException e)
{}
try
{
java.rmi.registry.LocateRegistry.createRegistry(1099);
System.out.println("RMI registry ready.");
}
catch (Exception e)
{
System.out.println("Exception starting RMI registry:");
e.printStackTrace();
}
try
{
RMIHandler = new ManagerRMIHandler();
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.rebind("HeadOfficeManager", RMIHandler);
System.err.println("Server ready");
}
catch (Exception e)
{
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
回答by bhuvanpavan
- Right click on application in eclipse and click on run configurations.
- Add virtual machine arguments as
-Djava.security.policy =java.policy.applet
. - Create a file, name it as
java.policy.applet
. Add below lines in that file.
grant { permission java.security.AllPermission; };
Save it and run the application.
- 右键单击 eclipse 中的应用程序,然后单击运行配置。
- 将虚拟机参数添加为
-Djava.security.policy =java.policy.applet
. - 创建一个文件,命名为
java.policy.applet
. 在该文件中添加以下几行。
grant { permission java.security.AllPermission; };
保存并运行应用程序。
This will give all security permissions to your Java application.
这将为您的 Java 应用程序授予所有安全权限。
回答by user207421
You have installed a SecurityManager and you haven't given yourself enough permissions in your .policy file to execute your code. The exception tells you what permission is missing, but there are probably more. Run your application with -Djava.security.debug=access,failure to see what other security problems there are.
您已经安装了 SecurityManager 并且您没有在 .policy 文件中给自己足够的权限来执行您的代码。异常告诉您缺少什么权限,但可能还有更多权限。使用 -Djava.security.debug=access,failure 运行您的应用程序以查看存在哪些其他安全问题。
But the real question here is why the security manager? You only need it in an RMI server, from the RMI point of view, if you are using the RMI codebase feature. Otherwise you should consider deleting it.
但这里真正的问题是为什么是安全管理器?从 RMI 的角度来看,如果您正在使用 RMI 代码库功能,那么您只需要在 RMI 服务器中使用它。否则你应该考虑删除它。
Also you must store the result of createRegistry
somewhere where it won't be garbage-collected, e.g. in a static variable. And once you've done that, the getRegistry()
call is redundant.
此外,您必须将结果存储在createRegistry
不会被垃圾收集的地方,例如在静态变量中。一旦你这样做了,getRegistry()
调用就是多余的。