SecurityManager getClassContext() Method


The java.lang.SecurityManager.getClassContext() method returns the current execution stack as an array of classes. The length of the array is the number of methods on the execution stack. The element at index 0 is the class of the currently executing method, the element at index 1 is the class of that method's caller, and so on.


Following is the declaration for java.lang.SecurityManager.getClassContext() method

protected Class[] getClassContext()



Return Value

This method returns the execution stack.




Our examples require that the permissions for each command is blocked. A new policy file was set that allows only the creating and setting of our Security Manager. The file is in C:/java.policy and contains the following text −

grant {
  permission java.lang.RuntimePermission "setSecurityManager";
  permission java.lang.RuntimePermission "createSecurityManager";
  permission java.lang.RuntimePermission "usePolicy";

The following example shows the usage of lang.SecurityManager.getClassContext() method.

package com.tutorialspoint;

public class SecurityManagerDemo extends SecurityManager {

   public static void main(String[] args) {

      // set the policy file as the system securuty policy
      System.setProperty("", "file:/C:/java.policy");

      // create a security manager
      SecurityManagerDemo sm = new SecurityManagerDemo();

      // set the system security manager

      // get the class context
      Class[] con = sm.getClassContext();

      // print the class context
      for (int i = 0; i < con.length; i++) {
         System.out.println("" + con[i]);

Let us compile and run the above program, this will produce the following result −

class com.tutorialspoint.SecurityManagerDemo