How I Fixed Gradle Test Error “getSubject is supported only if a security manager is allowed” on macOS

After setting up my development environment on a fresh macOS, I ran into a frustrating issue: my gradle tests were failing. I had encountered the error:

Thank me by sharing on Twitter 🙏

java.lang.UnsupportedOperationException: getSubject is supported only if a security manager is allowed

It took some digging to figure out what was going on, and as it turns out, the root cause was an incompatibility between the project code and the updated security manager in the latest version of OpenJDK that Gradle installs by default. This post outlines exactly what I did to resolve this issue and make Gradle work with the older JDK version 11.

The Problem: Tests Failing Due to a Security Manager Change

After setting everything up, I ran gradle test –debug to troubleshoot my failing tests.

ShellScript
gradle test --debug

Among a flood of logs, the following stood out:

java.lang.UnsupportedOperationException: getSubject is supported only if a security manager is allowed

This error was unfamiliar at first. But after some research, I discovered that my project relied on code that wasn’t compatible with recent changes to the security manager in OpenJDK 23 and later. The default brew install gradle command installs the latest available JDK, and that caused the problem. I needed Gradle to run using OpenJDK 11.

If you run into a similar issue, or if you have a legacy codebase that requires older Java behavior, this post will walk you through how I resolved it step-by-step by installing Gradle with Homebrew and configuring it to use OpenJDK 11.

Solution Overview

The main steps to fix this problem involve:

  1. Installing OpenJDK 11 alongside other versions of Java.
  2. Configuring your environment so Gradle uses the correct JDK version.
  3. Confirming everything works correctly by re-running tests.

Let’s go step-by-step through the solution.

1. Install OpenJDK 11 with Homebrew

Homebrew makes managing Java versions simple. First, we need to install OpenJDK 11. To do that, open a terminal and run:

ShellScript
brew install openjdk@11

This command downloads and installs OpenJDK 11, but it doesn’t yet make it the default Java version on your system. That’s because Homebrew installs it alongside other versions, like OpenJDK 17, which may already be installed by default.

2. Link OpenJDK 11 and Set It as Default for Gradle

Since multiple versions of Java can coexist on macOS, the key step is ensuring Gradle uses the right version. Instead of hardcoding the path to the JDK, I used macOS’s built-in java_home utility to dynamically point to version 11.

First, link OpenJDK 11:

ShellScript
brew link --force --overwrite openjdk@11

Then, set the JAVA_HOME environment variable to always use version 11. Open your shell configuration file (like .zshrc or .bash_profile) and add the following line:

export JAVA_HOME="$(brew --prefix openjdk@11)/libexec/openjdk.jdk/Contents/Home"

-- OR --

export JAVA_HOME=$(/usr/libexec/java_home -v 11)

This command ensures that Java 11 is used by Gradle, regardless of what other versions are installed on your machine.

After adding this, reload your shell configuration:

ShellScript
source ~/.zshrc  # or source ~/.bash_profile

To confirm that the correct Java version is now active, run:

ShellScript
java -version

You should see output like:

ShellScript
>java -version       
openjdk version "11.0.25" 2024-10-15
OpenJDK Runtime Environment Homebrew (build 11.0.25+0)
OpenJDK 64-Bit Server VM Homebrew (build 11.0.25+0, mixed mode)

This tells us that Java 11 is now the active version, which Gradle will use moving forward.

3. Install Gradle Using Homebrew

With the right Java version in place, the next step is installing Gradle. This is as simple as running:

ShellScript
brew install gradle

Homebrew will install the latest version of Gradle, but the key part is that it will now run with Java 11 because we configured the JAVA_HOME variable to point to that version.

4. Verify Gradle and Run Tests

Once Gradle is installed, it’s time to verify that everything is configured correctly. Run:

ShellScript
gradle -v

The output should confirm that Gradle is using the correct version of Java:

Plaintext
------------------------------------------------------------
Gradle 8.10.2
------------------------------------------------------------

Build time:    2024-09-23 21:28:39 UTC
Revision:      415adb9e06a516c44b391edff552fd42139443f7

Kotlin:        1.9.24
Groovy:        3.0.22
Ant:           Apache Ant(TM) version 1.10.14 compiled on August 16 2023
Launcher JVM:  11.0.25 (Homebrew 11.0.25+0)
Daemon JVM:    /opt/homebrew/Cellar/openjdk@11/11.0.25/libexec/openjdk.jdk/Contents/Home (no JDK specified, using current Java home)
OS:            Mac OS X 15.0.1 aarch64

Notice that the JVM version listed is 11.0.25. This means Gradle is now using OpenJDK 11, which resolves the security manager issue.

Now re-run your tests:

ShellScript
gradle test

If the issue was caused by an incompatibility with the newer JDK’s security manager, the tests should now pass without throwing the UnsupportedOperationException.

Conclusion

Dealing with compatibility issues after setting up a new macOS installation can be frustrating, but the root cause in this case turned out to be a mismatch between the project code and the security changes in newer versions of OpenJDK. By installing OpenJDK 11 and configuring Gradle to use it, I was able to resolve the issue and get my tests passing again.

This experience was a reminder of how important it is to pay attention to the Java version being used, especially when working with legacy code. Fortunately, tools like Homebrew make managing multiple versions of Java straightforward, and macOS’s java_home utility ensures that switching between them is painless. If you’re working on older projects, keeping these tricks in mind can save you hours of debugging time.

Share this:

Leave a Reply