Cannot get EclipseLink MOXy to work
Asked Answered
M

3

7

I'm new to JAXB and I want to change the default namespace prefix using EclipseLink MOXy. My package-info.java has the following code lines:

@javax.xml.bind.annotation.XmlSchema (
namespace="http://namespace.mysite.com/",
xmlns = { 
    @javax.xml.bind.annotation.XmlNs(prefix="myns", 
               namespaceURI="http://namespace.mysite.com/")
},
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED
)
package com.core.mymodel;

And my jaxb.properties file has the following line:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

I have added eclipselink.jar to the CLASSPATH, both package-info.java and jaxb.properties files are in the same package as my model classes, however, when I run the program I get the following error message:

javax.xml.bind.JAXBException: Provider org.eclipse.persistence.jaxb.JAXBContextFactory        not found
- with linked exception:
[java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory]
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)

I have tried this with EclipseLink versions 2.3.0 and 2.0.2 and still got the same error message. Does anyone happen to know what could be the problem?

Many thanks in advance

Mufti answered 25/7, 2011 at 14:46 Comment(5)
What environment are you running your application in Java SE, application server, OSGi?Solana
Hi Blaise, I'm using Java SE v1.6Mufti
Are you creating the JAXBContext on an array of classes or a String context path?Solana
I'm creating the JAXBContext on a String context path. Here is the code line: JAXBContext context = JAXBContext.newInstance("com.core.mymodel");Mufti
Just for clarification, "com.core.mymodel" is the name of the package that contains the model classes and the jaxb.index file.Mufti
S
4

You appear to have your jaxb.properties file correct based on the exception message:

javax.xml.bind.JAXBException: Provider org.eclipse.persistence.jaxb.JAXBContextFactory        not found
- with linked exception:
[java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory]
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)

To confirm that org.eclipse.persistence.jaxb.JAXBContextFactory is on your classpath can you try:

JAXBContext jc = JAXBContextFactory.createContext("com.core.mymodel", null);

To do a more basic confirmation can you run:

System.out.println(org.eclipse.persistence.Version.getVersion());

For More Information

Solana answered 25/7, 2011 at 17:40 Comment(4)
Eclipse throws a compile error: JAXBContextFactory cannot to be resolved.Mufti
I found the root of the problem. I added eclipselink.jar to the system classpath variable, however for some reason Eclipse wasn't able to find it. After adding it as a classpath variable to the Eclipse build path (Window -> Preferences -> Java -> Build Path -> Classpath Variables) and adding the classpath variable to the project's build path, I can now get the version of the installed EclipseLink.Mufti
EclipseLink MOXy does work perfectly with this simple project. I can configure namespace prefixes using @XmlSchema annotations. In this case, the JAXBContext is created on a class. However, with a more complex project that creates the JAXBContext on a String context path, I get a runtime error message that looks something like this: <br/> 'at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.marshalSingleValue(XMLCompositeObjectMappingNodeValue.java:176)' Any idea what could be the problem? Thanks for the help :-)Mufti
@Mufti - Could you open a bug for this issue, and we'll look into it: bugs.eclipse.org/bugs/enter_bug.cgi?product=EclipseLinkSolana
M
1

Simply specify the JAXBContext explicitly:

JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory
                .createContext(new Class[] {YourClass.class}, null);
Mohandas answered 2/9, 2022 at 12:45 Comment(0)
C
0

Use it as a System property

System.setProperty("javax.xml.bind.JAXBContextFactory", "org.eclipse.persistence.jaxb.JAXBContextFactory");
Chau answered 16/9, 2020 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.