10

I am having a bit of trouble -- a lot actually -- trying to figure out how get NetBeans to read my policy file for a particular application. Please take a look at the below code:

      public static void main(final String[] args)
      {
          System.setSecurityManager(new SecurityManager());
          System.setProperty("java.security.policy","file:/C:/Users/kBPersonal/Documents/NetBeansProjects/JAASTest/JAASTest.policy");

          EventQueue.invokeLater(new Runnable()
          {
              public void run()
              {
                  JFrame frame = new JAASFrame();
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setVisible(true);
              }
          });
      }

No matter what I do I keep getting the following error which lets me know that NetBeans is not reading my security.policy file (I even added it's location to the main security.policy file in the C:\Program Files (x86)\Java\jre6\lib\security\java.security). Incidentally, line 20 is where I try to set the System.setProperty("java.security.policy, ...)

     Exception in thread "main" java.security.AccessControlException: access denied (java.util.PropertyPermission java.security.policy write)
     at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
     at java.security.AccessController.checkPermission(AccessController.java:546)
     at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
     at java.lang.System.setProperty(System.java:725)
     at JAASTest.main(JAASTest.java:20)

Any and all help is greatly appreciated!

Honza
  • 974
  • 10
  • 18
Mike
  • 1,590
  • 8
  • 27
  • 41

4 Answers4

18

If you're using the System.setProperty() method to add your policy file, then make sure it's before you create the SecurityManager. I've used SecurityManager before with the System.setProperty() method, and calling it before I create the SecurityManager generally works.

Ataur Rahman Munna
  • 3,887
  • 1
  • 23
  • 34
Euphobia
  • 181
  • 2
  • 4
  • 1
    is there any way that we can check whether the given policy file is taken by the security manager, or how do we make sure that the given policy file is been taken in to consideration by the security manager – Kasun Siyambalapitiya Sep 27 '17 at 14:41
  • 1
    On my setup (openjdk 13.0.1), I had to call `Policy.getPolicy().refresh()` after the `setProperty` for it to be taken into account – Benoît Jan 06 '21 at 08:53
  • Thanks Benoit, that fixed my problem also. – Stefan Nov 19 '21 at 17:56
17

The easiest way to set a specific security policy is via a runtime argument. For example, this is what we do here for the same problem:

  1. Open "Project Properties -> Run"
  2. Select your runtime configuration
  3. Edit the "VM Options" for the runtime configuration
  4. Add the following:

    -Djava.security.manager -Djava.security.policy=src/dir1/dir2/important.policy

where you src/dir1/dir2/important.policy would be changed in your example to point at your file JAASTest.policy.

Ataur Rahman Munna
  • 3,887
  • 1
  • 23
  • 34
Bob Cross
  • 22,116
  • 12
  • 58
  • 95
  • When I do that it generates a slightly different error message : Exception in thread "main" java.security.AccessControlException: access denied (java.lang.RuntimePermission createSecurityManager) at java.security.AccessControlContext.checkPermission (AccessControlContext.java:323) at java.security.AccessController.checkPermission(AccessController.java:546) at java.lang.SecurityManager.checkPermission(SecurityManager.java:532) at java.lang.SecurityManager.(SecurityManager.java:282) at JAASTest.main(JAASTest.java:19) – Mike May 19 '11 at 16:52
  • Apparently, now it doesnt like my Security Manager declaration? Go figure :) – Mike May 19 '11 at 17:06
  • You know what, once I removed the line: System.setProperty("java.security.policy","file:/C:/Users/kBPersonal/Documents/NetBeansProjects/JAASTest/JAASTest.policy"); it is at least reading my JAASTest.policy file and my Frame comes up. I think trying to figure out why it is still not giving me the access I need is the subject of another question! Thanks Bob! – Mike May 19 '11 at 17:27
5

Add security policy before setting your system security manager.

according to your given code first add

System.setProperty("java.security.policy","file:/C:/Users/kBPersonal/Documents/NetBeansProjects/JAASTest/JAASTest.policy");

then

System.setSecurityManager(new SecurityManager());
Ataur Rahman Munna
  • 3,887
  • 1
  • 23
  • 34
  • is there any way that we can check whether the given policy file is taken by the security manager, or how do we make sure that the given policy file is been taken in to consideration by the security manager – Kasun Siyambalapitiya Sep 27 '17 at 14:42
  • 1
    You can check the system property by the key `java.security.policy` and see if it set or not. If not set then you get null. Follow this line `String prop = System.getProperty("java.security.policy");` – Ataur Rahman Munna Sep 28 '17 at 04:32
  • On my setup (openjdk 13.0.1), I had to call `Policy.getPolicy().refresh()` after the `setProperty` for it to be taken into account – Benoît Jan 06 '21 at 08:53
0

Although it's not ideal and it's not a definitive solution, running "rmiregistry &" from the location where your .class files reside, would solve this issue.

iammyr
  • 271
  • 5
  • 13