Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception in thread "main" javax.xml.bind.JAXBException: Provider com.sun.xml.bind.v2.ContextFactory could not be instantiated

I'm trying to test JAXB unmarshaller/marshaller. Here is my code

JAXBContext context = JAXBContext.newInstance(ClientUser.class.getPackage().getName());

And a code of my entity

@XmlRootElement(name = "user")
public class ClientUser {
    private String name;

    public ClientUser() {}

    public ClientUser(String name) {
        this.name = name;
    }

    @XmlElement(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Even if I add to the entity class a factory class

@XmlRegistry
class ObjectFactory {
    ClientUser createPerson() {
        return new ClientUser();
    }
}

I'm still keep getting this exception

Exception in thread "main" javax.xml.bind.JAXBException: Provider com.sun.xml.bind.v2.ContextFactory could not be instantiated: javax.xml.bind.JAXBException: "com.example.ws.poc.entity" doesnt contain ObjectFactory.class or jaxb.index
 - with linked exception:
[javax.xml.bind.JAXBException: "com.example.ws.poc.entity" doesnt contain ObjectFactory.class or jaxb.index]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:146)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:335)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:431)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:394)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:298)

How can I fix this error?

like image 837
Aaron Avatar asked Oct 25 '14 01:10

Aaron


1 Answers

A JAXB implementation doesn't do package scanning. If you boot strap from a package name, JAXB is going to look for an ObjectFactory (annotated with @XmlRegistry) or a jaxb.index file that contains short class names each on a new line.

If you don't have these two items you can create the JAXBContext on the domain classes themselves.

JAXBContext jc = JAXBContext.newInstance(Foo.class, Bar.class);
like image 72
bdoughan Avatar answered Oct 12 '22 19:10

bdoughan