tags:

views:

2542

answers:

2

I'm getting a javax.xml.bind.JAXBException: "doesnt contain ObjectFactory.class or jaxb.index" while trying to create a JAXBContext using JAXBContext.newInstance(String contextPath). I'm guessing there's a "usual" way to create and maintain a jaxb.index file?

A: 

Make sure you're passing the correct class to the method. Assuming your XML root element is XMLRoot, you would call it as:

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

Also make sure that you're using the correct version of the JAXB compiler (xjc) for the version of Java you're running. JAXB-generated classes from the old compiler won't work properly with Java 6's JAXB, giving the same error.

Andrew Coleson
@Andrew Coleson - wouldn't you want to put the packagae name in there, not the root element tag name?
Mark Lewis
A: 

Try this way,

JAXBContext context = JAXBContext.newInstance(new Class[] {your.package.Test.class});

Also, make sure that you added the @XmlRootElement to the Test class.

@XmlRootElement class Test { private String ...; private int ......; }

also make sure that you are using java 1.5

All the best, Hari Krishna

Hari