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+
}
Call
What 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
Parameter
Description
sm
The security manager to install, or null to remove the current one.
Started normally. First part: Java 18 and newer; second part: Java 8 to 17 (Java 17 also prints a WARNING on System.err, not shown).
Not supported: UnsupportedOperationException
--- Java 8 to 17 ---
Installed: true
Removed again: true
How It Works
A manager that never says no.AllowAll overrides checkPermission with an empty body, so every check passes, even removing it again.
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.
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);
}
}
Output
Usage: tool <file>
Exit status requested: 2
Processing data.csv
Exit status requested: 0
How It Works
Exiting through an interface.runTool calls exiter.exit(...), never System.exit. A real program passes System::exit, which fits ExitHandler.
A test double records instead.RecordingExit only stores the status, so the JVM keeps running. With no arguments the tool asks for status 2.
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()).
Run on Java 17 or newer with java SystemSetSecurityManagerStatus, without any -Djava.security.manager option.
Deprecated: true
For removal: true
Since: 17
allow flag given: false
How It Works
Finding the method.getMethod looks up the public method by name and parameter type and returns a Method object.
Reading the annotation.@Deprecated is kept at runtime, so getAnnotation returns it, with forRemoval()true and since() "17".
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+.