有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java LogManager忽略已启用的断言

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

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

407号线的拦截器

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

应该但不要抛出AssertionError

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


共 (1) 个答案

  1. # 1 楼答案

    你必须使用-esaenable 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和没有-esa的情况下应该运行的行为:

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

    另一个选项是,通过将命令行设置为:-ea -ea:java.util.logging.LogManager,只启用用户代码的断言和日志管理器