Enable the Java SecurityManager with AllPermission
In which context are you running your code above ?
from command line with a simple JVM or inside a webapp running on top of some JavaEE container? On which OS? with which JVM (Oracle, OpenJDK, IBM J9...) and which version?
If you're running from command line, have a look at the java.policy
file located in your JVM installation path. Its content may narrow your grants and thus prevent you from accessing this particular system variable ?
I was able to recreate your case with an extra Policy.getPolicy()
before the Policy.setPolicy()
call, the reason why it affects the behaviour is that with the get policy call, you trigger a default policy creation, and permissions from java.policy
are set, but without a setSecurityManager()
they are not activated, that is the reason when you do a custom AllPermission
policy set, you still get a "java.util.PropertyPermission" "java.home" "read"
issue, for many of such default policies are not overridden with the set policy. Very confusing structure indeed.
Policy.getPolicy();
Policy.setPolicy(policyWithAllPermission);
System.setSecurityManager(new SecurityManager());
System.out.println(System.getProperty("java.home"));
// results in 'access denied ("java.util.PropertyPermission" "java.home" "read")'
But if you use the following custom policy;
Policy allPermissionPolicy = new Policy() {
@Override
public boolean implies(ProtectionDomain domain, Permission permission) {
return true;
}
};
It overrides all permission definitions, and lets all actions through, a possible fix for this confusion.