如何查找正在使用的JAXP实现以及它是从哪里加载的?

10 浏览
0 Comments

如何查找正在使用的JAXP实现以及它是从哪里加载的?

我想提供关于正在使用的JAXP实现以及它是从哪个JAR文件加载的诊断信息。

实现这一目标的一种方法是创建一个例如DocumentBuilderFactory的实例,然后检查该类的属性:

private static String GetJaxpImplementation() {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    Class c = documentBuilderFactory.getClass();
    Package p = c.getPackage();
    CodeSource source = c.getProtectionDomain().getCodeSource();
    return MessageFormat.format(
            "正在使用JAXP实现''{0}'' ({1}) 版本 {2} ({3}){4}",
            p.getName(),
            p.getImplementationVendor(),
            p.getSpecificationVersion(),
            p.getImplementationVersion(),
            source == null ? "。" : "从: " + source.getLocation() + "加载");
}

是否有更好的方法可以实现这一目标,可能无需创建DocumentBuilderFactory

0