它就在那里,在包中,它应该是索引的。不过,当我打电话给
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.inder.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<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;
}
从我的previous experience中,我猜测这与运行它的OSGi容器的类加载机制有关。不幸的是,我仍然有点超出我的能力范围。
发布于 2009-06-25 10:35:16
编辑2:
我曾经在我的应用程序中遇到过类似的奇怪的类加载问题。如果我将其作为普通应用程序运行,一切正常,但当我将其作为Windows Service调用时,它开始出现ClassNotFoundExceptions故障。分析表明,线程以某种方式将其类加载器设置为空。我通过在线程上设置SystemClassLoader解决了这个问题:
// ...
thread.setContextClassLoader(ClassLoader.getSystemClassLoader());
thread.start();
// ...
我不知道你的容器是否允许这种改变。
https://stackoverflow.com/questions/1043109
复制相似问题