Java System setSecurityManager() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Deprecated (Java 17)

Definition

In Java, System.setSecurityManager(sm) installs sm as the program's security manager, which the JDK asks before sensitive actions like reading files or exiting. null removes the current one, if allowed.

It is deprecated for removal since Java 17. From Java 18 it throws UnsupportedOperationException unless started with -Djava.security.manager=allow; from Java 24 (JEP 486) it always throws.

A static method of the java.lang.System class, available since the first version of Java (JDK 1.0). Call it on the class name, as in System.setSecurityManager(...). No import is needed, because classes in java.lang are always visible. Deprecated for removal since Java 17.

Syntax

On new Java, wrap the call:

Java
try {
    System.setSecurityManager(myManager);
} catch (UnsupportedOperationException e) {
    // Java 18+ without -Djava.security.manager=allow, or any Java 24+
}
CallWhat it does
System.setSecurityManager(sm)Java 8 to 17: installs sm (Java 17 prints a warning).
System.setSecurityManager(sm)Java 18 to 23: throws unless started with -Djava.security.manager=allow.
System.setSecurityManager(sm)Java 24 and newer: always throws UnsupportedOperationException.

Since Java 1.0. Never install a restrictive manager to experiment: it can stop your own program from reading files or exiting.

Library declarations
Java
// java.lang.System
@Deprecated(since="17", forRemoval=true)
public static void setSecurityManager(SecurityManager sm)

Parameters

ParameterDescription
smThe security manager to install, or null to remove the current one.

Return Value

Return ValueDescription
voidNothing.

Compare with Similar Methods

Related calls and replacements:

MethodHow it differs
getSecurityManager()Reads the current manager, usually null.
exit(status)Wrap it behind an interface instead of blocking it with a manager.
-Djava.security.manager=allowOpt-in on Java 18-23.

Java Examples

Example 1 — Try Installing a Permissive Manager

The manager below allows everything, so it cannot break the program. What happens depends only on the Java version.

Java
import java.security.Permission;

public class SystemSetSecurityManagerTry {
    static class AllowAll extends SecurityManager {
        @Override
        public void checkPermission(Permission perm) {
            // allow everything
        }
    }

    @SuppressWarnings("removal")
    public static void main(String[] args) {
        try {
            System.setSecurityManager(new AllowAll());
            System.out.println("Installed: " + (System.getSecurityManager() != null));
            System.setSecurityManager(null);
            System.out.println("Removed again: " + (System.getSecurityManager() == null));
        } catch (UnsupportedOperationException e) {
            System.out.println("Not supported: UnsupportedOperationException");
        }
    }
}

How It Works

  1. A manager that never says no. AllowAll overrides checkPermission with an empty body, so every check passes, even removing it again.
  2. Modern Java refuses. On Java 18 and newer, started normally, the first call throws UnsupportedOperationException and the catch prints the first line. Java 24+ refuses even with -Djava.security.manager=allow.
  3. Older Java accepts. On Java 8 to 17 the manager is installed, so getSecurityManager() is not null; setSecurityManager(null) then removes it.

Example 2 — Replace "Block System.exit" with an Exit Handler

Tests used a security manager to stop System.exit. Today, let the code exit through an interface that tests can replace.

Java
public class SystemSetSecurityManagerExitHandler {
    interface ExitHandler {
        void exit(int status);
    }

    static class RecordingExit implements ExitHandler {
        int lastStatus = -1;

        @Override
        public void exit(int status) {
            lastStatus = status;
        }
    }

    static void runTool(String[] args, ExitHandler exiter) {
        if (args.length == 0) {
            System.out.println("Usage: tool <file>");
            exiter.exit(2);
            return;
        }
        System.out.println("Processing " + args[0]);
        exiter.exit(0);
    }

    public static void main(String[] args) {
        RecordingExit recorder = new RecordingExit();
        runTool(new String[0], recorder);
        System.out.println("Exit status requested: " + recorder.lastStatus);
        runTool(new String[] {"data.csv"}, recorder);
        System.out.println("Exit status requested: " + recorder.lastStatus);
    }
}

How It Works

  1. Exiting through an interface. runTool calls exiter.exit(...), never System.exit. A real program passes System::exit, which fits ExitHandler.
  2. A test double records instead. RecordingExit only stores the status, so the JVM keeps running. With no arguments the tool asks for status 2.
  3. Both paths checked. With "data.csv" the tool reports progress and asks for 0. No security manager is involved, so every Java version behaves the same.

Example 3 — Read the @Deprecated Annotation at Runtime

Reflection shows how the JDK marks this method. Compiling needs Java 9 or newer (for forRemoval() and since()).

Java
import java.lang.reflect.Method;

public class SystemSetSecurityManagerStatus {
    @SuppressWarnings("removal")
    public static void main(String[] args) throws NoSuchMethodException {
        Method method = System.class.getMethod("setSecurityManager", SecurityManager.class);
        Deprecated info = method.getAnnotation(Deprecated.class);
        System.out.println("Deprecated: " + (info != null));
        System.out.println("For removal: " + (info != null && info.forRemoval()));
        System.out.println("Since: " + (info == null ? "-" : info.since()));
        System.out.println("allow flag given: " + "allow".equals(System.getProperty("java.security.manager")));
    }
}

How It Works

  1. Finding the method. getMethod looks up the public method by name and parameter type and returns a Method object.
  2. Reading the annotation. @Deprecated is kept at runtime, so getAnnotation returns it, with forRemoval() true and since() "17".
  3. Is the escape hatch on? The last line checks the java.security.manager property for "allow"; it is unset in a normal run, so it is false.

Frequently Asked Questions

From Java 18 the Security Manager is disabled by default. Start with -Djava.security.manager=allow on Java 18 to 23; on Java 24 and newer it cannot be enabled at all.
It is permanently disabled since Java 24 (JEP 486), but the classes still exist for compatibility.
Route exits through your own interface, as in the exit handler example, or use a separate process.
Yes, with a removal warning on System.err.
Yes: SecurityException on old versions if the current manager forbids it, and UnsupportedOperationException on Java 24+.

Back to the method index

See all 25 System method guides grouped by what they do.

Method index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful