public class S3ButcketTest
{
public static void main(String args[])
{
System.setSecurityManager(new RMISecurityManager());
ForkJoinPool pool = new ForkJoinPool(1);
System.out.println("main: "+Policy.getPolicy());
pool.execute(new Runnable()
{
@Override
public void run()
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("pool: "+Policy.getPolicy());
}
});
pool.shutdown();
try
{
pool.awaitTermination(1000, TimeUnit.SECONDS);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
使用VM参数在两个不同的JDK版本(8和11)上运行:-Djava.security.policy = C:\ test_system.policy
test_system.policy内容:
grant {
permission java.security.AllPermission;
};
与JDK11一起运行时的输出:
main: sun.security.provider.PolicyFile@4cb2c100
Exception in thread "ForkJoinPool-1-worker-3" java.security.AccessControlException: access denied ("java.security.SecurityPermission" "getPolicy")
at java.base/java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.base/java.security.AccessController.checkPermission(AccessController.java:895)
at java.base/java.lang.SecurityManager.checkPermission(SecurityManager.java:322)
at java.base/java.security.Policy.getPolicy(Policy.java:159)
at S3ButcketTest$1.run(S3ButcketTest.java:30)
at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1426)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)
与JDK8一起运行时的输出:
main: sun.security.provider.PolicyFile@1b84c92
pool: sun.security.provider.PolicyFile@1b84c92
我们将JDK从8升级到11,突然我们面临这个问题。为什么会发生这种情况?有解决办法吗?