为什么以编程方式激活断言时 "assert false" 不会导致 AssertionError?

Why is "assert false" not causing AssertionError when assertion are activated programmatically?

如果我在 Oracle documentation

之后激活断言
ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
ClassLoader.getSystemClassLoader().setPackageAssertionStatus("richtercloud.java.assertion.ignored", true);
System.out.println(String.format("desired assertion status: %b",
        NewMain.class.desiredAssertionStatus()));
assert false;
System.out.println("assertion has been ignored");

在 class richtercloud.java.assertion.ignored.NewMainmain 方法中,我从打印的语句中看到 assert false 不会像它那样导致 AssertionError如果我将 NewMain 打包到 JAR 中并将其 运行 与 java -ea -jar java-assertion-ignored-1.0-SNAPSHOT-jar-with-dependencies.jar richtercloud.java.assertion.ignored.NewMain.

打包在一起

关于以编程方式启用断言的其他问题only suggest to not use assertions,这显然不是解决方案。

如果我对文档的理解正确,您必须在加载 class 之前设置断言状态;在此示例中,您已经加载了 class,因此您的 setDefaultAssertionStatus(true) 无效。

Quoting from the documentation(我的斜体):

Each class loader maintains a default assertion status, a boolean value that determines whether assertions are, by default, enabled or disabled in new classes that are subsequently initialized by the class loader.

因此,设置默认断言状态只会影响随后加载的classes,不会影响当前正在执行的。