Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get JAXBContext?

Tags:

java

jaxb

In my java project i have a jar that i generated with some classes. I am able to instantiate the instance of the class that is in my jar:

Alert a = new Alert();

But wen i try to do this:

JAXBContext context  = JAXBContext.newInstance(Alert.class);

I get run time exception like this:

java.lang.InternalError: Error occured while invoking reflection on target classes. Make sure all referenced classes are on classpath: interface javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter Exception: null

Any idea what could be the issue? Thank you

like image 361
user3174776 Avatar asked Oct 01 '22 22:10

user3174776


1 Answers

This happened to me as I had defined XmlAdapter implementations that were in a different JAR and not on the classpath. When trying to create the JAXBContext, it needs these adapters used by the JAXB type. Now that I figured this out, the message makes more sense, but is still very cryptic. XmlJavaTypeAdapter is the annotation interface and the code fails to call the value() method of the annotation as it returns Class<? extends XmlAdapter>, a class type which is not defined on the classpath.

If you look at the code that throws the exception, this is impossible:

public Class More ...getClassValue(Annotation a, String name) {
  try {
    return (Class)a.annotationType().getMethod(name).invoke(a);
  } catch (IllegalAccessException e) {
    // impossible
    throw new IllegalAccessError(e.getMessage());
  } catch (InvocationTargetException e) {
    // impossible
    throw new InternalError(e.getMessage());
  } catch (NoSuchMethodException e) {
    throw new NoSuchMethodError(e.getMessage());
  }
}

Would be nice if the cause of the exception was not lost.

like image 86
Dave H Avatar answered Oct 11 '22 10:10

Dave H