3

I've been trying to use JAXB binding to read xml files I have in java apps. So far, I've been able to do the following steps:

  • generate .xsd for the .xml I have
  • generate java classes using the jaxb-compiler.cmd (I'm running on windows)
  • use the generated 'device.java', 'objectfactory.java', and the jaxb.properties
  • create the java app that performs the following steps:

    File file = new File("path/to/my.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(testxml.Device.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Device device = (Device) jaxbUnmarshaller.unmarshal(file);
    System.out.println(device.getName());
    

Note that I had issues with this app as well, it was complaining on the JAXBContext::newInstance with javax.xml.bind.JAXBException - with linked exception:

    [java.lang.ClassNotFoundException:org.eclipse.persistence.jaxb.JAXBContextFactory]

To fix this I followed recommendations from the forums to convert the project to the JPA project, and in the process to define user library for EclipseLink (linking it to eclipselink v 2.6.2 I downloaded)

The project I'm working on is an Eclipse RCP. It needs to read the contents of the XML file. I have tried the same steps, creating the sample RCP project in Eclipse Mars, adding my xml java class to it, and got the same error. Trying the same process from the package explorer: Configure>>Convert To JPA did not fix the problem.

I checked the .classpath to verify that the eclipselink is listed, and it is. Always fails on the first call to get the JAXB context.

Following other recommendations from forums, I tried:

jaxbContext = JAXBContextFactory.createContext("my_package", Device.class.getClassLoader());

and that gives the java.lang.NoClassDefFoundError on the same class.

If I look at the project in eclipse's project explorer, i can see the eclipselink library, and I can drill into eclipselink.jar and find the offending 'org.eclipse.persistence.jaxb.JAXBContextFactory' class in there.

So far, looked at Lars Vogel's web site, tutorials are great and RCP generation works as long as I don't try to use JAXB in it. Also, Blaise Doughan's blogs and comments make perfect sense except that this combination (RCP + EclipseLink) does not work for me at all :(

Any ideas on how to solve this are appreciated.

srdanz
  • 31
  • 2

0 Answers0