LogManager 忽略启用的断言

LogManager ignores enabled assertions

在调试器中逐步执行 java.util.logging.LogManager 代码时,我发现 由于第 1301 行出现异常

try (final InputStream in = new FileInputStream(fname)) {

第 407 行的 catch 块

assert false : "Exception raised while reading logging configuration: " + ex;

应该但不要抛出 AssertionError

虚拟机选项:-ea -Djava.util.logging.config.file=logging.properties.

你必须使用 -esa enable assertions in all system classes。根据 -ea[:[packagename]...|:classname] 的文档:

The -enableassertions (-ea) option applies to all class loaders and to system classes (which do not have a class loader). There is one exception to this rule: if the option is provided with no arguments, then it does not apply to system classes. This makes it easy to enable assertions in all classes except for system classes. The -enablesystemassertions option provides a separate switch to enable assertions in all system classes.

这是一个测试用例,用于验证您应该 运行 使用和不使用 -esa:

的行为
public static void main(String[] args) {
    System.out.println(LogManager.class.desiredAssertionStatus());
}

另一种选择是通过将命令行设置为:-ea -ea:java.util.logging.LogManager

只为用户代码和 LogManager 启用断言