就在那儿,它应该在包中建立索引。 不过,当我打电话时
JAXBContext jc = JAXBContext.newInstance("my.package.name");
我得到一个JAXBException说
“ my.package.name”不包含ObjectFactory.class或jaxb.index
尽管它确实包含两者。
有用的,但不是我想要的,是
JAXBContext jc = JAXBContext.newInstance(my.package.name.SomeClass.class);
来自其他人的这个问题出现在很多邮件列表和论坛上,但似乎没有得到答案。
我在OpenJDK 6上运行此程序,因此我获得了源程序包,并将调试器步进到该库中。 它首先查找jaxb.properties,然后查找系统属性,但找不到任何一个,它尝试使用com.sun.internal.xml.bind.v2.ContextFactory创建默认上下文。 在那里,抛出了异常(在ContextFactor.createContext(String ClassLoader, Map)内部),但是由于源不在这里,所以我看不到发生了什么。
预计到达时间:
从ContentFactory的源代码来看,我在这里发现,这可能是一段代码无法按预期工作:
/**
* Look for jaxb.index file in the specified package and load it's contents
*
* @param pkg package name to search in
* @param classLoader ClassLoader to search in
* @return a List of Class objects to load, null if there weren't any
* @throws IOException if there is an error reading the index file
* @throws JAXBException if there are any errors in the index file
*/
private static List loadIndexedClasses(String pkg, ClassLoader classLoader) throws IOException, JAXBException {
final String resource = pkg.replace('.', '/') + "/jaxb.index";
final InputStream resourceAsStream = classLoader.getResourceAsStream(resource);
if (resourceAsStream == null) {
return null;
}
根据我以前的经验,我猜想这与正在运行的OSGi容器的类加载机制有关。不幸的是,我在这里还不够深入。