Not sure what error means? javax.xml.bind.JAVAXBException
Hi all!
I have compiled several .java files into .class files with no errors. All of the .java source files are located in my working directory and i unpacked any .jar's that were provided into the same directory prior to compiling, so all dependency imports should be present.
When i attempt to run my .class file, i get errors and i'm having a hard time understanding what they mean.
Can someone help point me in the right direction? Thanks so much for any info!
Code:
C:\java_test>java -classpath . com.amazonservices.mws.sellers.samples.GetService
StatusSample
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.amazonservices.mws.sellers.MarketplaceWebServiceSellersClient.<cl
init>(MarketplaceWebServiceSellersClient.java:111)
at com.amazonservices.mws.sellers.samples.GetServiceStatusSample.main(Ge
tServiceStatusSample.java:69)
Caused by: javax.xml.bind.JAXBException: Provider com.sun.xml.internal.bind.v2.C
ontextFactory could not be instantiated: javax.xml.bind.JAXBException: "com.amaz
onservices.mws.sellers.model" doesnt contain ObjectFactory.class or jaxb.index
- with linked exception:
[javax.xml.bind.JAXBException: "com.amazonservices.mws.sellers.model" doesnt con
tain ObjectFactory.class or jaxb.index]
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at com.amazonservices.mws.sellers.MarketplaceWebServiceSellersClient.<cl
init>(MarketplaceWebServiceSellersClient.java:107)
... 1 more
Caused by: javax.xml.bind.JAXBException: "com.amazonservices.mws.sellers.model"
doesnt contain ObjectFactory.class or jaxb.index
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Sou
rce)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
... 6 more
Re: Not sure what error means? javax.xml.bind.JAVAXBException
I think i narrowed this down to these lines:
Code:
/** Initialize JAXBContext and Unmarshaller **/
static {
try {
jaxbContext = JAXBContext.newInstance(
"com.amazonservices.mws.sellers.model",
MarketplaceWebServiceSellers.class.getClassLoader());
} catch (JAXBException ex) {
throw new ExceptionInInitializerError(ex);
}
unmarshaller = new ThreadLocal<Unmarshaller>() {
@Override
protected synchronized Unmarshaller initialValue() {
try {
return jaxbContext.createUnmarshaller();
} catch (JAXBException e) {
throw new ExceptionInInitializerError(e);
}
}
};
}
but am unsure if i'm correct. From my understanding, this error is related to the wrong class loader being used?
Quote:
14 down vote accepted
OK, this took quite some digging, but the answer is not that surprising and not even that complicated:
JAXB can't find jaxb.index, because by default, newInstance(String) uses the current thread's class loader (as returned by Thread.getContextClassLoader()). This doesn't work inside Felix, because the OSGi bundles and the framework's threads have separate class loaders.
The solution is to get a suitable class loader from somewhere and use newInstance(String, ClassLoader). I got a suitable class loader from one of the classes in the package that contains jaxb.index, a sensible choice for flexibility reasons probably is ObjectFactory:
ClassLoader cl = my.package.name.ObjectFactory.class.getClassLoader ();
JAXBContext jc = JAXBContext.newInstance("my.package.name", cl);
Maybe you could also get at the class loader that the Bundle instance is using, but I couldn't figure out how, and the above solution seems safe to me.
found this answer on stackoverflow.com but I cna't figure out how to implement this change into my program. From this, it makes be believe my program is unable to locate a jaxb.index file? and that to fix this I need to adjust the code so it looks to the correct class? Can someone help me interpret this error correctly so i can make the necessary changes!
Thanks so much!