In Java, System.getSecurityManager() returns the security manager installed for the program, or null if there is none. A security manager was an object that could forbid actions such as reading files or calling System.exit, mainly for applets and sandboxes.
Normal programs never had one, so the answer is almost always null. The whole mechanism is deprecated for removal since Java 17 (JEP 411), and since Java 24 (JEP 486) it cannot be turned on at all, so this method always returns null there.
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.getSecurityManager(...). No import is needed, because classes in java.lang are always visible. Deprecated for removal since Java 17.
Syntax
The traditional pattern checks for a manager before asking it:
Java
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkRead("data.txt"); // throws SecurityException if not allowed
}
Call
What it does
System.getSecurityManager()
The installed manager, usually null.
System.getSecurityManager() == null
true in normal programs and always on Java 24+.
sm.checkExit(status)
Old way to ask whether System.exit is allowed.
@SuppressWarnings("removal")
Hides the compiler's removal warning on Java 17+.
Since Java 1.0; deprecated for removal since Java 17. On Java 17 and newer the compiler warns about it; @SuppressWarnings("removal") silences that warning and is simply ignored on Java 8.
Library declarations
Java
// java.lang.System
@Deprecated(since="17", forRemoval=true)
public static SecurityManager getSecurityManager()
Parameters
Parameter
Description
(none)
getSecurityManager() takes no arguments.
Return Value
Return Value
Description
SecurityManager
The installed security manager, or null if none is installed.
One of the actions a manager used to be able to block.
java -Djava.security.manager
Old command-line way to start with a manager (an error on Java 24+).
Java Examples
Example 1 — Check Whether a Security Manager Is Installed
The simplest use: ask for the manager and report whether one exists, without printing the object itself.
Java
public class SystemGetSecurityManagerCheck {
@SuppressWarnings("removal")
public static void main(String[] args) {
SecurityManager sm = System.getSecurityManager();
System.out.println("Security manager installed: " + (sm != null));
System.out.println("Running without one: " + (sm == null));
}
}
Terminal output
Started normally with java SystemGetSecurityManagerCheck, without any -Djava.security.manager option.
Security manager installed: false
Running without one: true
How It Works
The usual answer. A program started with plain java has no security manager, so the call returns null. On Java 24 and newer it returns null no matter how the program starts.
Two views of one fact.sm != null prints false and sm == null prints true; printing booleans keeps the output identical on every computer.
Quiet compilation.@SuppressWarnings("removal") on main stops the Java 17+ compiler from warning that SecurityManager and this method are going away.
Example 2 — The Classic "Check If a Manager Exists" Guard
Older library code asked the manager for permission only when one was installed. With no manager, the check is simply skipped.
Java
public class SystemGetSecurityManagerGuard {
static int checksPerformed = 0;
@SuppressWarnings("removal")
static void checkCanRead(String file) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkRead(file);
checksPerformed++;
}
}
public static void main(String[] args) {
checkCanRead("data.txt");
checkCanRead("config.txt");
System.out.println("Reads allowed: 2");
System.out.println("Permission checks performed: " + checksPerformed);
}
}
Terminal output
Started normally with java SystemGetSecurityManagerGuard, without any -Djava.security.manager option.
Reads allowed: 2
Permission checks performed: 0
How It Works
Ask only if someone is in charge.checkCanRead fetches the manager and calls checkRead only when it is not null; that method would throw SecurityException to forbid the read.
No manager, no checks. Both calls find null, return without asking anyone, and never throw, so the program reaches both print statements.
The counter tells the story.checksPerformed only grows inside the if, so it stays 0. This is why removing the Security Manager does not break such code: the guard was already skipped.
Example 3 — Ask Whether System.exit Would Be Allowed
A helper that answers "may this program exit?" the old way: no manager means yes, otherwise the manager's checkExit decides.
Java
public class SystemGetSecurityManagerExit {
@SuppressWarnings("removal")
static boolean mayExit(int status) {
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
return true;
}
try {
sm.checkExit(status);
return true;
} catch (SecurityException e) {
return false;
}
}
public static void main(String[] args) {
System.out.println("May exit with 0: " + mayExit(0));
System.out.println("May exit with 1: " + mayExit(1));
}
}
Terminal output
Started normally with java SystemGetSecurityManagerExit, without any -Djava.security.manager option.
May exit with 0: true
May exit with 1: true
How It Works
Short-circuit on null. The first if returns true straight away when no manager exists, so checkExit is never reached in this run.
What a manager would do. With a manager installed, checkExit(status) returns quietly to allow exiting or throws SecurityException to refuse, which the helper turns into false.
Any status is fine. Exit codes 0 and 1 both get true, because nothing restricts the program. Test tools once used a manager like this to stop code under test from exiting.
Frequently Asked Questions
Because no security manager is installed, which is the normal case. From Java 24 a security manager cannot be enabled at all, so it is always null.
Yes. It is deprecated for removal since Java 17 (JEP 411), together with SecurityManager and setSecurityManager.
Nothing inside the JVM. Use operating-system accounts and permissions, containers, or separate processes to isolate untrusted code.
Annotate the method or class with @SuppressWarnings("removal"). Java 8 ignores that annotation value.
It keeps working while the method exists, because it simply sees null. It will need changes once the method is removed.