How do I use a jaxb.index file?
Asked Answered
C

3

26

I'm getting

javax.xml.bind.JAXBException: "org.example.mypackage" 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.

Ceil answered 22/5, 2009 at 19:49 Comment(0)
T
38

The jaxb.index file is just a listing of the classes in the containing package that have JAXB annotations.

Each line in the file is a class's simple name, not its fully qualified name.

You can read more here: http://cmaki.blogspot.com/2007/09/annotated-jaxb-classes.html

Twigg answered 22/12, 2010 at 2:35 Comment(1)
By classified, do you mean fully qualified class name? Foo would be class name and com.a.b.c.Foo would be fully qualified class name.Koo
T
15

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

Twinned answered 3/6, 2010 at 16:4 Comment(0)
G
4

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.

Glucinum answered 5/6, 2009 at 0:1 Comment(1)
@Andrew Coleson - wouldn't you want to put the packagae name in there, not the root element tag name?Riker

© 2022 - 2024 — McMap. All rights reserved.