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
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.