Java:在私有策略文件中应用权限时遇到问题

Java:在私有策略文件中应用权限时遇到问题,java,netbeans,securitymanager,Java,Netbeans,Securitymanager,我正试图遵循oracle教程中关于在Java应用程序中使用安全管理器授予或拒绝对系统资源的访问权的内容: 我想授予我的NetBeans项目访问“java.home”和“user.home”属性的权限。 为此,我生成了一个名为$HOME/.java.policy的私有策略文件: grant codeBase "file:/home/myself/NetBeansProjects/" { permission java.util.PropertyPermission "user.home", "

我正试图遵循oracle教程中关于在Java应用程序中使用安全管理器授予或拒绝对系统资源的访问权的内容:

我想授予我的NetBeans项目访问“java.home”和“user.home”属性的权限。 为此,我生成了一个名为$HOME/.java.policy的私有策略文件:

grant codeBase "file:/home/myself/NetBeansProjects/" {
  permission java.util.PropertyPermission "user.home", "read";
  permission java.util.PropertyPermission "java.home", "read";
};
其中codeBase指向我的NetBeans项目所在的目录(/home/imf/NetBeansProjects/)

在java.security中,我定义了以下url:

# The default is to have a single system-wide policy file,
# and a policy file in the user's home directory.
policy.url.1=file:${java.home}/lib/security/java.policy
policy.url.2=file:${user.home}/.java.policy
现在,我的一个NetBeans项目正在尝试读取“java.home”和“user.home”属性:

System.out.println("About to get user.home property value");
s = System.getProperty("user.home", "not specified");
System.out.println("  Your user home directory is: " + s);


System.out.println("About to get java.home property value");
s = System.getProperty("java.home", "not specified");
System.out.println("  Your JRE installation directory is: " + s);
但是,如果我尝试在启用安全管理器的情况下从命令行运行Java程序,我仍然会得到一个AccessControlException:

$ pwd
/home/myself/NetBeansProjects/GetProps/src

$ java -Djava.security.manager getprops.GetProps

About to get user.home property value
Caught exception java.security.AccessControlException: access denied  ("java.util.PropertyPermission" "user.home" "read")
我想知道问题是否存在于我的私有策略文件中的代码基路径中

我正在测试的系统和软件: NetBeans IDE 7.4(构建201310111528) Java:1.7.0_45;OpenJDK客户端VM 24.45-b08 运行时:OpenJDK运行时环境1.7.0_45-b31
系统:Linux版本3.12.3-1-ARCH,运行在i386上;UTF-8;es_es(nb)

假设项目代码实际位于子目录中,您可能需要:

grant codeBase "file:/home/myself/NetBeansProjects/-" {

(递归地向所有子目录授予权限)。

您是否尝试使用
java-Djava.security.manager-Djava.security.policy=$HOME/.java.policy getprops.getprops运行程序以确保策略文件被真正使用?是的,我得到了相同的结果端注释,为什么要使用安全管理器运行?你真的需要吗?当你说“从命令行”:它在Netbeans内部工作吗?这就解决了它,谢谢。出于测试目的,我只是在学习oracle教程