Gradle build process hangs when System.setSecurityManager(new SecurityManager()); is called

I have been trying to load a custom security manager to refrain developers from writing to and reading from test files in unit tests. In order to do that, I wrote a simple extension to java.lang.SecurityManager.

First I tried to set the SecurityManager using @BeforeClass annotation.

    public static void setSecurityManager() throws Exception {

        System.out.println("setting a new security manager");


        System.out.println("a new security manager is set");

        System.out.println("test class is initialized");


When I run the test suite via gradle test it just hangs during the test. But I’m positive that I can run these tests via Intellij and I got my expected results there.

After I used the test task of Java Plugin and did the below:

test {
    logger.warn("running tests from gradle")
    SecurityManager manager = System.getSecurityManager();

    if (manager == null) {
        println("there is no security manager!");
    System.setSecurityManager(new SecurityManager());

This led to the same hang problem again.
Then I wondered if it has anything to do with Java Plugin so I wrote a simple task below:

task showSystemProperty << {
    println("security manager: " +""))

    SecurityManager manager = System.getSecurityManager();
    System.setSecurityManager(new SecurityManager());
    println(manager == null ? "no manager" : manager.toString())

I run the task with gradle showSystemProperty and I again got the same hang problem. So what am I doing wrong here? Or is this also related to this issue: ?