Home » Java programming language

Java SecurityManager checkPermission() method with example

SecurityManager Class checkPermission() method: Here, we are going to learn about the checkPermission() method of SecurityManager Class with its syntax and example.
Submitted by Preeti Jain, on December 14, 2019


    public void checkPermission(Permission  perm);
    public void checkPermission(Permission  perm, Object cntxt);

SecurityManager Class checkPermission() method

  • checkPermission() method is available in java.lang package.
  • checkPermission(Permission perm) method invokes checkPermission of AccesController for the requested access, indicated by the specified permissions.
  • checkPermission(Permission perm, Object cntxt) method invokes checkPermission of AccesControlContext for the given security context is the access granted to the resource, indicated by the specified permissions when cntxt is an instance of AccessControlContext.
  • checkPermission(Permission perm), checkPermission(Permission perm, Object cntxt) methods may throw an exception at the time of granting permission.
  • checkPermission(Permission perm):
    • SecurityException – This exception may throw when the access is denied on the security policy held currently.
    • NullPointerException – This exception may throw when the given parameter is null.
  • checkPermission(Permission perm, Object cntxt):
    • SecurityException – This exception may throw when the calling thread is not allowed to access the resource by the given permission or when the security cntxt(context) is not an object of AccessControlContext.
    • NullPointerException – This exception may throw when the given first parameter is null.
  • These are non-static methods, it is accessible with the class object only and, if we try to access these methods with the class name then we will get an error.


  • In the first case, Permission perm - This parameter represents the requested permission.
  • In the second case, Permission perm, Object cntxt
    • Permission perm – Similar as defined in the first case.
    • Object cntxt – This parameter represents the system-specific security context.

Return value:

The return type of this method is void, it returns nothing.


// Java program to demonstrate the example 
// of checkPermission() method of SecurityManager class


public class CheckPermission extends SecurityManager {
    public static void main(String[] args) {
        Permission perm = new FilePermission("getProperties().doc", "read,write");
        AccessControlContext acc = AccessController.getContext();

        // By using setProperty() method is to set the policy property 
        // with security manager
        System.setProperty("", "file:/C:/java.policy");

        // Instantiating a CheckPermission object
        CheckPermission cp = new CheckPermission();

        // By using setSecurityManager() method is to set the
        // security manager

        // By using checkPermission(Permission) method is to
        // check that restricted permission

        // By using checkPermission(Permission,Object) method is to
        // check that restricted permission when cntxt is an instance
        // of AccessControlContext
        cp.checkPermission(perm, acc);

        // Display the message 


Exception in thread "main" access denied ("" "getProperties().doc" "read,write")
	at java.base/
	at java.base/
	at java.base/java.lang.SecurityManager.checkPermission(
	at CheckPermission.main(


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.