简体   繁体   中英

How to grant file read/write permission in a Security Manager policy file for variable parent directories

I'm learning to use Security Manager, and I'm getting this error when I run my unit tests:

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")

It's simple enough grant permission to that directory. The problem is I run this code on different computers. Ideally, I'd like to do something like this:

permission java.io.FilePermission "*/NetBeans/Cache/7.4/-", "write";

But apparently SecurityManager doesn't recognize wildcard characters at the beginning of the path. I've tried using both an asterisk and a dash. Neither works.

Basically, I'd like to get my tests to run without needing to hard-code an absolute path. Is there another way to achieve this?

You can use 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.

Could you set:

permission java.io.FilePermission "${user.home}/AppData/Local/NetBeans/-", "write";

to get what you wanted?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM