Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the security.policy file for a specific application from within NetBeans?

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!

like image 855
Mike Avatar asked May 19 '11 16:05

Mike


2 Answers

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());
like image 61
Ataur Rahman Munna Avatar answered Oct 24 '22 11:10

Ataur Rahman Munna


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.

like image 35
Euphobia Avatar answered Oct 24 '22 12:10

Euphobia