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

StackOverflow https://stackoverflow.com/questions/21613422

  •  08-10-2022
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top