deniz.in

Markets

Weather

Loading weather

· via dev.to (home feed)

Java 25 SecurityManager removal breaks a long-standing trick for testing System.exit

Java 24 permanently disabled the SecurityManager, and teams moving to the Java 25 LTS are finding test suites that relied on it to intercept System.exit failing on day one. The fix is to stop calling System.exit outside main.

Java 25 SecurityManager removal breaks a long-standing trick for testing System.exit

A test-killing call, and the trick that tamed it

A command-line tool that terminates the JVM with System.exit when it receives bad arguments is harmless in production but disastrous under test: a single call ends the entire run, not just the failing case. A recent dev.to post by Serguey Asael Shinder walks through why this old problem is suddenly resurfacing, and it comes down to the removal of the SecurityManager.

How tests used to block System.exit

According to the post, the standard workaround was a small SecurityManager installed by the test harness. It intercepted exit attempts and threw an exception instead, which the test caught and used to inspect the exit code before carrying on. The pattern was common enough that libraries were written to support it, and plugin hosts used the same mechanism to sandbox code they loaded dynamically. Many codebases, the author suggests, still contain one of these helpers and have long since forgotten about it.

What the JDK did

The SecurityManager was deprecated for removal in Java 17, and JEP 486, delivered in Java 24, switched it off permanently. As the post describes, it can no longer be enabled at startup or installed while the program runs, and System.setSecurityManager now throws UnsupportedOperationException.

The timing is what turns this into an active problem rather than a footnote: Java 25 is the long-term release that most teams on Java 21 are migrating to now. For those codebases, the author writes, this is not a warning about the future but the next upgrade, and it fails in the test suite on the first day, usually in a helper class nobody has opened in years.

The fix is a design change

The constructive part of the post is that the workaround's disappearance pushes code toward what the author argues was always the better design: never call System.exit deep inside application code. Instead, return an exit code or throw a dedicated exception, and make main the single place that ends the process. Tests then exercise the logic directly rather than main, and assert on the value it returns, with no interception trick needed.

For programs that load code they do not trust, the post's answer is to move it out of the same JVM entirely: run it in a separate process or a container and let the operating system enforce the limits. That aligns with the JDK team's stated position, which the author summarises as the SecurityManager being rarely used for sandboxing in practice and expensive for the platform to keep maintaining.

Auditing before the upgrade

The post closes with practical advice for anyone preparing the jump: search the codebase and build files for SecurityManager, checkPermission, checkExit and the java.security.manager flag. The number of hits, the author says, is the size of the work hiding behind a deprecation warning that most people stopped reading years ago. His closing argument is that a "for removal" label is effectively a schedule rather than a suggestion.

Why it matters

The removal touches far more than a niche API. It invalidates a testing idiom that made its way into helper classes and third-party libraries across many Java projects, and teams skipping Java 24 and moving straight from 21 to the 25 long-term release will meet it as a hard failure rather than a warning. It also formalises a shift in how Java handles isolation: in-process sandboxing through the JVM is gone, and process- or container-level boundaries are the replacement. Finally, it is a reminder that deprecation warnings carry delivery dates, and the cheapest time to act on them is while the code still compiles.

  • #java
  • #testing
  • #migration
  • #jvm
  • #security-manager

Related posts