Java Data Type Tutorial - Java SecurityManager .checkPermission (Permission perm, Object context)








Syntax

SecurityManager.checkPermission(Permission perm, Object context) has the following syntax.

public void checkPermission(Permission perm,   Object context)

Example

In the following code shows how to use SecurityManager.checkPermission(Permission perm, Object context) method.

/*from  www  .ja v  a2s. c o  m*/
import java.io.FilePermission;
import java.security.AccessControlContext;
import java.security.AccessController;

public class Main extends SecurityManager {

   public static void main(String[] args) {
      AccessControlContext con = AccessController.getContext();

      System.setProperty("java.security.policy", "file:/C:/java.policy");

      Main sm = new Main();

      System.setSecurityManager(sm);

      sm.checkPermission(new FilePermission("test.txt", "read,write"), con);

      System.out.println("Allowed!");
   }
}