它在那里,在包中,它应该索引。还是,当我打电话
JAXBContext jc = JAXBContext.newInstance("my.package.name");
我得到一个JAXBException说
“my.package.name” doesnt contain ObjectFactory.class or 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)),但我不能看到发生了什么,因为源不在这里。
ETA:
从ContentFactory的源代码判断,我发现here,这可能是一段代码无法按预期工作:
/**
* 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;
}
从我的previous experience,我猜这是与运行在OSGi容器的类加载机制。不幸的是,我仍然有点离我的深度。