JaxbRepresentation gives error “doesnt contain ObjectFactory.class or jaxb.index”

后端 未结 8 1246
再見小時候
再見小時候 2021-02-02 08:11

I am trying to create a sample test application which converts an object to JaxbRepresentation. But when I try to run this, it gives me an error.

Main.java file

8条回答
  •  醉梦人生
    2021-02-02 08:49

    I got this error because of a ClassLoader issue, and I was able to solve it by explicitly passing the ClassLoader that JAXB should use, so this:

    JAXBContext.newInstance(com.myexample.test.ObjectFactory.class.getPackage().getName());
    

    gave an error, but worked properly when using:

    JAXBContext.newInstance(com.myexample.test.ObjectFactory.class.getPackage().getName(),
                            com.myexample.test.ObjectFactory.class.getClassLoader());
    

    which is probably similar to user3243752's answer, I bet that JAXB is automatically choosing the ClassLoader from the passed in class when using the #newInstance(Class... classesToBeBound) method signature.

提交回复
热议问题