我正在学习使用安全管理器,当我运行单元测试时遇到此错误:
Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\Users\[username]\AppData\Local\NetBeans\Cache\7.4\executor-snippets\junitvmwatcher1469887727677239882.properties" "write")
授予该目录权限非常简单。问题是我在不同的计算机上运行此代码。理想情况下,我想做这样的事情:
permission java.io.FilePermission "*/NetBeans/Cache/7.4/-", "write";
但显然 SecurityManager 无法识别路径开头的通配符。我尝试过同时使用星号和破折号。两者都不起作用。
基本上,我想让我的测试运行而不需要硬编码绝对路径。还有其他方法可以实现这一目标吗?
请您参考如下方法:
您可以使用property expansion in policy files :
For example, permission java.io.FilePermission "${user.home}", "read"; will expand "${user.home}" to use the value of the "user.home" system property.
您可以设置:
permission java.io.FilePermission "${user.home}/AppData/Local/NetBeans/-", "write";
得到你想要的东西?