它应该在要索引的包中,但当我调用时,它就在那里。
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)
内),但我无法看到发生了什么,因为源代码不在此处。
ETA:
根据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<Class> 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;
}